首页 > 解决方案 > 我在使用 webpack 构建 Node 应用程序时遇到问题

问题描述

让我描述一下我的问题。我使用 ES6 开发了一个 Node.js 应用程序,它是一个使用多个 Node 模块的 REST API,尤其是来自 google-cloud,因为我使用的是 Google Cloud Vision 和 Translate API。

到现在为止都没有问题,一切都按预期工作,但是当我想在 Windows Server 上将它作为服务运行时出现了问题。我在这里找到了一种使用节点模块“node-windows”的方法。

我制作了该帖子中的服务脚本,该服务已安装并显示在 Windows 服务列表中,但是当我单击启动时,它会立即停止。

经过一些分析后,我记得我正在使用 ES6,它需要被转换为 ES5 才能像标准 Node 脚本一样工作,所以我认为用 webpack 构建我的整个应用程序将为我解决这个问题,但不完全是,我得到了我的包。使用 webpack 生成的 Node.js 没有任何错误(只是一些警告),然后当我尝试使用它运行它时node ./bundle.js会返回如下错误:

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

虽然我在我的 webpack 配置文件中制定了一个规则来支持 .proto 文件。

这是我的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"
        }
      }
    ]
  }
};

在这个级别,我不知道如何将这些 google-cloud .proto 文件集成到我的 bundel.js 中,有人可以指导我吗?谢谢。

这是来自 @google-cloud 模块中的 grpc.js 的代码,它试图解析 .proto 文件路径:

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);
    };

标签: javascriptnode.jswebpack

解决方案


推荐阅读