2

Let me describe my problem. I have developed a Node.js application with ES6, it is a REST API using several Node modules especially from google-cloud because I am using Google Cloud Vision and Translate APIs.

Until now there is no problem, everything works as expected, but things got wrong when I wanted to run it as a service on a Windows Server. I found a way to do it here using the Node module "node-windows".

I made the service script like in that post and the service got installed and shown in the Windows services list, but when I click to start it stops immediately.

After some analyzing I remembered that I am using ES6 that needs to be transpiled to ES5 to work like a standard Node script, so I thought that building my whole app with webpack will solve that for me, but not exactly, I got my bundle.js generated with webpack without any error (just some warnings), then when I try to run it with node ./bundle.js it returns errors like :

Error: The include '/protos/google/cloud/vision/v1/image_annotator.proto' was not found.

Though I made a rule in my webpack config file to support .proto files.

This is my webpack.config.js :

module.exports = {
  target: "node",
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      },
      {
        test: /\.json$/,
        exclude: /node_modules/,
        use: {
          loader: "json-loader"
        }
      },
      {
      test: /\.proto$/,
          use: {
            loader: "pbf-loader"
          }
      },
      {
        test: /\.html$/,
        use: {
          loader: "html-loader"
        }
      }
    ]
  }
};

At this level, I have no idea how to make those google-cloud .proto files to be integrated in my bundel.js, can someone please guide me ? thanks.

This is the code from grpc.js inside the @google-cloud module that tries to resolve the .proto files paths:

GoogleProtoFilesRoot.prototype.resolvePath = function (originPath, includePath) {
        originPath = path.normalize(originPath);
        includePath = path.normalize(includePath);
        // Fully qualified paths don't need to be resolved.
        if (path.isAbsolute(includePath)) {
            if (!fs.existsSync(includePath)) {
                throw new Error('The include `' + includePath + '` was not found.');
            }
            return includePath;
        }
        if (COMMON_PROTO_FILES.indexOf(includePath) > -1) {
            return path.join(googleProtoFilesDir, includePath);
        }
        return GoogleProtoFilesRoot._findIncludePath(originPath, includePath);
    };
Ahmed
  • 41
  • 3
  • 1
    I've never used webpack, but I see that your other loaders are being wrapped in a `use` block (i.e. `use: { loader: "json-loader" }`, yet your protobuf loader isn't wrapped in that `use` block. Does changing that make any difference? According to the documentatoin @ https://webpack.js.org/concepts/loaders/ you should also be able to do `use: "pbf-loader"` – nbokmans Nov 16 '18 at 10:16
  • can you add the code where you are including the file ? – Pranay Tripathi Nov 16 '18 at 10:23
  • @nbokmans thank you for your quick response I didn't notice that sorry, but changing it doesnt make any difference. – Ahmed Nov 16 '18 at 10:42
  • @PranayTripathi thanks for your response, I included it in the post – Ahmed Nov 16 '18 at 11:20
  • @Ahmed I dont think this is a loader issue but rather a path issue. Can you try to require the proto file from the given path ? Webpack loader comes into picture if you are trying to import a module or require a module. Please check your path generation as well. – Pranay Tripathi Nov 16 '18 at 12:10
  • @PranayTripathi thank you, but running the application in a normal way (without webpack) doesnt pop any problem and the Google APIs just works. How can I check my path generation ? , I am really new at this sorry – Ahmed Nov 16 '18 at 13:11

0 Answers0