node.js - Webpack 和 GoogleApis 端点路径问题

标签 node.js webpack google-api google-api-client webpack-2

我正在尝试在 webpack 项目中使用 googleapis。每当我调用

static getApi = () => {
    google.sheets('v4');
}

我收到以下错误:无法加载端点 ../apis/sheets("v4"): 找不到模块“。” 源自 googleapis/lib/googleapis.js 的第 50 行,基本上是 var Endpoint = require(endpointPath);

我尝试查看 endpointPath,但它恰好是正确的:node_modules/googleapis/apis/sheets/v4

我的 webpack.config.js 如下所示:

module.exports = {
   entry: ['babel-polyfill','./src/index.js'],
   target: 'async-node', // Improved performance in node version 6+
   node: {
     __dirname: true
   },
   output: {
     filename: './dist/bundle.js',
     libraryTarget: 'commonjs2'
   },
   module: {
     rules: [
      {
    test: /\.(graphql|gql)$/,
    exclude: /node_modules/,
    loader: 'graphql-tag/loader'
  },
  {
    test: /\.js$/,
    exclude: /(node_modules)/,
    use: {
      loader: 'babel-loader',
        options: {
          presets: ['env'],
          plugins: [require('babel-plugin-transform-class-properties')]
        }
      }
    }
  ]
  },
  devtool: 'source-map'
}

删除

node: {
  __dirname: true
},

导致 ENOENT: no such file or directory, scandir '/apis' 源自上述 googleapis.js 的第 62 行

最佳答案

根据这个github issue comment ,应将 googleapis Node 客户端排除在任何服务器端捆绑机制之外。

googleapis (google-api-nodejs-client) will work in Node.js. Excluding googleapis from any server-side bundle (just let Node's module system load it for you) is the best option.

关于node.js - Webpack 和 GoogleApis 端点路径问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44244359/

相关文章:

typescript - ES6 typescript 将所有导出的常量导入为数组

android - 为什么 build.gradle 中导入存储库的顺序决定了项目的成败?

google-api - 如何避免 Gmail API 集成的安全评估

node.js - 如何在Koa中为静态文件设置base?

node.js - ZeroMQ 和 IPC 的区别

webpack - loader/src/app/app.component.scss 中的错误没有返回字符串

javascript - 错误: 3 INVALID_ARGUMENT: Name 'Hello' does not match patterns DialogFlow

javascript - Fabric.JS 与 Node.JS - 导出为 PNG/JPEG

node.js - NodeJS 存储的是需要解密和读取的加密数据库密码,而不是用户密码?

javascript - 找不到入口模块 : Error: Can't resolve './src'