要求@ google-cloud /语言打破webpack('模块未找到')

时间:2017-08-15 14:23:16

标签: webpack google-cloud-platform webpack-dev-server google-cloud-nl

我对这个有点困惑,所以这里有:

我尝试使用搜索栏进行React应用程序,该搜索栏将调用Google的自然语言API(https://cloud.google.com/natural-language/),作为其中的一部分我当然安装了&# 39; @谷歌云/语言&#39;包。但是,当我尝试在任何文件中要求它(我的具体情况是const Language = require('@google-cloud/language');),然后尝试运行webpack-dev-server时,它无法找到...我想< em>我的模块的任何?它实际上只是一个巨大的墙壁&#34;未找到模块&#34;错误,例如

Error in ./~/grpc/~/node-pre-gyp/lib/info.js
Module not found: 'aws-sdk' in /home/amberb/projects/project-ui/node_modules/grpc/node_modules/node-pre-gyp/lib

 @ ./~/grpc/~/node-pre-gyp/lib/info.js 14:14-32

Error in ./~/grpc/~/node-pre-gyp/lib/publish.js
Module not found: 'aws-sdk' in /home/amberb/projects/project-ui/node_modules/grpc/node_modules/node-pre-gyp/lib

 @ ./~/grpc/~/node-pre-gyp/lib/publish.js 17:14-32

Error in ./~/grpc/~/node-pre-gyp/lib/unpublish.js
Module not found: 'aws-sdk' in /home/amberb/projects/project-ui/node_modules/grpc/node_modules/node-pre-gyp/lib

 @ ./~/grpc/~/node-pre-gyp/lib/unpublish.js 15:14-32

Error in /usr/lib/~/npm/bin/npm-cli.js
Module parse failed: /usr/lib/node_modules/npm/bin/npm-cli.js Unexpected character '#' (1:0)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected character '#' (1:0)
 @ /usr/lib/~/npm/lib/npm.js 453:4-32

Error in /usr/lib/~/npm/~/opener/opener.js
Module parse failed: /usr/lib/node_modules/npm/node_modules/opener/opener.js Unexpected character '#' (1:0)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected character '#' (1:0)
 @ /usr/lib/~/npm/lib/auth/sso.js 4:13-30

Error in /usr/lib/~/npm/~/JSONStream/index.js
Module parse failed: /usr/lib/node_modules/npm/node_modules/JSONStream/index.js Unexpected character '#' (1:0)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected character '#' (1:0)
 @ /usr/lib/~/npm/lib/search/all-package-metadata.js 12:17-38

Error in /usr/lib/~/npm/~/npm-registry-client/lib/initialize.js
Module not found: 'json' in /usr/lib/node_modules/npm/node_modules/npm-registry-client/lib

 @ /usr/lib/~/npm/~/npm-registry-client/lib/initialize.js 5:10-36

Error in /usr/lib/~/npm/~/validate-npm-package-name/index.js
Module not found: 'json' in /usr/lib/node_modules/npm/node_modules/validate-npm-package-name

 @ /usr/lib/~/npm/~/validate-npm-package-name/index.js 4:15-34

Error in /usr/lib/~/npm/~/pacote/lib/util/opt-check.js
Module not found: 'json' in /usr/lib/node_modules/npm/node_modules/pacote/lib/util

(这会持续一段时间)

只要我不包含@ google-cloud / language,就不会发生这种情况。我似乎无法找到关于可能做到这一点的任何解释。有没有人知道我至少可以尝试的东西?

编辑添加:在this question出现推荐相关问题后,我尝试添加

externals: {
      '@google-cloud/language': 'commonjs @google-cloud/language'
  }

到我的webpack配置文件中,但这只会导致我的页面无法加载并给我webpack:///external_%22@google-cloud/language%22?:1 Uncaught ReferenceError: require is not defined。我仍然对Webpack有点新鲜,所以我很肯定我错过了什么,但我不确定是什么。

1 个答案:

答案 0 :(得分:2)

如果其他人带着这样的问题来到这里,问题最终是我试图包含这个不应该包含在前端前端的软件包。我们将它拆分为后端API网关,并从前端调用它。作为一项规则,任何为您必须提供身份验证并可能产生使用成本的API调用的功能都应该拆分为单独的后端服务/功能。