Webpack和GoogleApis端点路径问题

时间:2017-05-29 14:00:58

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

我试图在网络包项目中使用googleapis。每当我调用

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

我得到以下Error: Unable to load endpoint ../apis/sheets("v4"): Cannot find module "." 源自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行

1 个答案:

答案 0 :(得分:1)

根据此github issue comment,应从任何服务器端捆绑机制中排除googleapis节点客户端。

  

googleapis(google-api-nodejs-client)将在Node.js中运行。排除   来自任何服务器端捆绑的googleapis(只需让Node的模块系统   为你加载它是最好的选择。