0

Whenever I run my docker image, I keep getting an error in terms of incompatibility between systems/architecture for node...

I tried this command:

npm rebuild --target=8.1.0 --target_platform=linux --target_arch=x64 --target_libc=glibc --update-binary

But the problem still persists...

Here are my files for more context:

Full error After I try to run my docker image

Error: Failed to load gRPC binary module because it was not installed for the current system
Expected directory: node-v83-linux-x64-glibc
Found: [node-v72-darwin-x64-unknown]
This problem can often be fixed by running "npm rebuild" on the current system
Original error: Cannot find module '/usr/src/app/node_modules/grpc/src/node/extension_binary/node-v83-linux-x64-glibc/grpc_node.node'

Require stack:
- /usr/src/app/node_modules/grpc/src/grpc_extension.js
- /usr/src/app/node_modules/grpc/src/client_interceptors.js
- /usr/src/app/node_modules/grpc/src/client.js
- /usr/src/app/node_modules/grpc/index.js
- /usr/src/app/booksServer.js
    at Object.<anonymous> (/usr/src/app/node_modules/grpc/src/grpc_extension.js:53:17)
    at Module._compile (internal/modules/cjs/loader.js:1236:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1257:10)
    at Module.load (internal/modules/cjs/loader.js:1085:32)
    at Function.Module._load (internal/modules/cjs/loader.js:950:14)
    at Module.require (internal/modules/cjs/loader.js:1125:19)
    at require (internal/modules/cjs/helpers.js:75:18)
    at Object.<anonymous> (/usr/src/app/node_modules/grpc/src/client_interceptors.js:144:12)
    at Module._compile (internal/modules/cjs/loader.js:1236:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1257:10) {
  code: 'MODULE_NOT_FOUND'

Dockerfile (running my gRPC node server)

FROM node:latest
WORKDIR /usr/src/app
COPY package*.json ./
COPY . /usr/src/app
RUN npm install
EXPOSE 30043
CMD ["node", "booksServer.js"]

Booksserver.js (GRPC Server)

// const PROTO_PATH = "../protos/books.proto";
const path = require('path');
const PROTO_PATH = path.join(__dirname, '../protos/books.proto');
const grpc = require("grpc");
const protoLoader = require("@grpc/proto-loader");
const express = require("express");
const controller = require("./booksController.js");
const app = express();
app.use(express.json());

const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
  keepCase: true,
  longs: String,
  enums: String,
  arrays: true,
});

const booksProto = grpc.loadPackageDefinition(packageDefinition);

const { v4: uuidv4 } = require("uuid");

const server = new grpc.Server();

server.addService(booksProto.BooksService.service, {
  CreateBook: (call, callback) => {
    console.log("call to CreateBook");

    //sample will take the call information from the client(stub)
    const book = {
      title: call.request.title,
      author: call.request.author,
      numberOfPages: call.request.numberOfPages,
      publisher: call.request.publisher,
      id: call.request.id,
    };

    controller.createBook(book);

    let meta = new grpc.Metadata();
    meta.add("response", "none");
    call.sendMetadata(meta);

    callback(
      null,
      //bookmodel.create
      {
        title: `completed for: ${call.request.title}`,
        author: `completed for: ${call.request.author}`,
        numberOfPages: `completed for: ${call.request.numberOfPages}`,
        publisher: `completed for: ${call.request.publisher}`,
        id: `completed for: ${call.request.id}`,
      }
    );
  },
  GetBooks: (call, callback) => {
    console.log("call to GetBooks");
    // read from database
    let meta = new grpc.Metadata();
    meta.add('response', 'none');
    call.sendMetadata(meta);

    controller.getBooks(callback);
  },
  GetBookByID: (call, callback) => {
    console.log("call to GetBookByID");
    
    let sampleID = {id: 100};

    let meta = new grpc.Metadata();
    meta.add('response', 'none');
    call.sendMetadata(meta);

    controller.getBookByID(sampleID, callback);
  },
  DeleteBook: (call, callback) => {
    //sample will take the call information from the client(stub)
    const bookID = {
      id: call.request.id,
    };

    //this actually sends data to booksController.
    controller.deleteBook(bookID);

    let meta = new grpc.Metadata();
    meta.add('response', 'none');
    call.sendMetadata(meta);

    // delete from database
    callback(null, { message: "DELETED" });
  },
});

server.bind("127.0.0.1:30043", grpc.ServerCredentials.createInsecure());
console.log("booksServer.js running at http://127.0.0.1:30043");

console.log("call from books server");

server.start();

TheRoadLessTaken
  • 531
  • 2
  • 7
  • 15
  • Are you developing on a mac? You might try adding `node_modules` to the `.dockerignore` file so that it is not copied into the Docker image and all of the packages are installed in the Docker image. – jkr Jul 30 '20 at 00:43
  • I actually realized I had to change the target to the correct (latest node version), but I still get this issue: ``` Error: Failed to load gRPC binary module because it was not installed for the current system Expected directory: node-v72-linux-x64-glibc Found: [node-v72-darwin-x64-unknown] This problem can often be fixed by running "npm rebuild" on the current system Original error: Cannot find module '/usr/src/app/node_modules/grpc/src/node/extension_binary/node-v72-linux-x64-glibc/grpc_node.node' Require stack: ``` It seems like there's still a difference in Operating systems.. – TheRoadLessTaken Jul 30 '20 at 00:50
  • @jakub, yes I'm developing on mac, and I just added the .dockerignore file... It seems to work becauses I have a different error now :)! Error is: /usr/src/app/node_modules/protobufjs/src/root.js:104 throw err; ^ Error: ENOENT: no such file or directory, open '/usr/src/protos/books.proto' at Object.openSync (fs.js:440:3) at Object.readFileSync (fs.js:342:35) at fetch (/usr/src/app/node_modules/protobufjs/src/root.js:172:34) at Root.load (/usr/src/app/node_modules/protobufjs/src/root.js:206:13) ..... – TheRoadLessTaken Jul 30 '20 at 01:02
  • ISSUE RESOLVED: It turns out I had to change the directory of my protos haha. Stupid mistake. Huge shoutout to @jakub for telling me to add the node_modules in .dockerignore file (still iffy as to why that's the case...) Also this command worked, but please note that 'target' means the node module version you want it to be. (in my case node is version 12.14.0) npm rebuild --target=12.14.0 --target_platform=linux --target_arch=x64 --target_libc=glibc --update-binary – TheRoadLessTaken Jul 30 '20 at 01:21

0 Answers0