我正在尝试在我的 React 应用程序中使用 AWS Translate,它似乎需要大量 Node 填充。 AWS SDK 是否仅供 Node.js 使用,还是我只需要安装 12 个 polyfill?我正在查看的 AWS 文档 似乎表明它可以与客户端 React 代码一起使用,而且我没有看到任何有关 polyfill 或浏览器解决方法的注释。
进口:
import { TranslateClient, TranslateTextCommand } from '@aws-sdk/client-translate';
这是我在安装和编译时看到的 24 个类似错误之一:
ERROR in ./node_modules/@smithy/util-stream/dist-cjs/sdk-stream-mixin.js 7:15-30
Module not found: Error: Can't resolve 'util' in 'node_modules/@smithy/util-stream/dist-cjs'
BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.
If you want to include a polyfill, you need to:
- add a fallback 'resolve.fallback: { "util": require.resolve("util/") }'
- install 'util'
If you don't want to include a polyfill, you can use an empty module like this:
resolve.fallback: { "util": false }
@ ./node_modules/@smithy/util-stream/dist-cjs/index.js 81:24-55
@ ./node_modules/@smithy/smithy-client/dist-cjs/index.js 134:25-55
@ ./node_modules/@aws-sdk/client-translate/dist-cjs/index.js 121:27-59
@ ./app/utils/AmazonTranslate.js 12:0-82 65:23-38 77:30-50
webpack compiled with 24 errors
如果我将以下内容添加到我的 webpack 配置中:
fallback: {
child_process: false,
crypto: false,
fs: false,
http: false,
http2: false,
https: false,
os: false,
path: false,
process: false,
stream: false,
url: false,
util: false
}
它编译时没有错误,但是当我尝试执行代码时,我得到了这个:
TypeError: Cannot destructure property 'readFile' of 'fs_1.promises' as it is undefined.
node-polyfill-webpack-plugin
,但仍然存在6个错误,涉及fs
、child_process
和http2
。