要求 @google-cloud/language 会破坏 webpack ('Module Not Found')

Requiring @google-cloud/language breaks webpack ('Module Not Found')

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

我正在尝试做一个带有搜索栏的 React 应用程序,它将调用 Google 的自然语言 API (https://cloud.google.com/natural-language/),作为其中的一部分,我当然安装了'@google-cloud/language'包。但是,当我尝试在任何文件中要求它(我的具体情况是 const Language = require('@google-cloud/language');)然后尝试 运行 webpack-dev-server 时,它找不到......我想 任何 我的模块?它实际上只是一堵巨大的 "Module not found" 错误墙,例如

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'
  }

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

如果其他人带着这样的问题来到这里,问题最终是我试图在前端中包含这个本不应该真正包含在前端的包。我们将其拆分到我们的后端 API 网关中,并从前端调用它。通常,进行任何 API 调用且您必须提供身份验证且可能产生使用成本的功能应拆分到单独的后端 service/function.