我对这个有点困惑,所以这里是:
我正在尝试做一个带有搜索栏的React应用程序,该应用程序将调用Google的自然语言API(https://cloud.google.com/natural-language/(,作为其中的一部分,我当然安装了"@google云/语言"包。但是,当我尝试在任何文件中需要它(我的具体情况是const Language = require('@google-cloud/language');
(,然后尝试运行 webpack-dev-server 时,它找不到......我想我的任何模块?从字面上看,它只是一堵巨大的"找不到模块"错误的墙,例如
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云/语言时,就不会发生这种情况。我似乎找不到任何关于可能这样做的解释。有谁知道我至少可以尝试的东西?
编辑补充:在这个问题作为推荐的相关问题出现后,我尝试添加
externals: {
'@google-cloud/language': 'commonjs @google-cloud/language'
}
在我的 webpack 配置文件中,但这只会导致我的页面无法加载并给我webpack:///external_%22@google-cloud/language%22?:1 Uncaught ReferenceError: require is not defined
.我对 Webpack 仍然有些陌生,所以我肯定我错过了一些东西,但我不确定是什么。
如果其他人遇到这样的问题,问题最终是我试图包含这个不应该真正包含在前端前端的包。我们将其拆分为后端 API 网关,并从前端内部调用它。通常,进行任何 API 调用的功能(您必须为其提供身份验证(以及可能产生使用成本的功能应拆分为单独的后端服务/功能。