AWS SDK Javascript v3需要节点polyfills

问题描述 投票:0回答:1

我正在尝试在我的 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

reactjs amazon-web-services aws-sdk aws-sdk-js
1个回答
0
投票

AWS Translate 依赖流式传输进行实时翻译,因此 sdk 客户端使用一些节点特定的流式传输功能。

Web 中的流式传输和节点中的流式传输使用不同的底层工具。例如,Web 使用 WebSocket,而节点有自己的原语,如 stream

大多数情况下,Web 应用程序都有一个访问 AWS 的中间服务器。如果您不想泄露 API 密钥和其他敏感信息,那么这是必要的。有一些有效的用例,例如可以在其中包含敏感信息的本地托管 Web 应用程序,但这不是 AWS 的目标用户,因此可能无法获得所有 SDK 客户端的支持。

© www.soinside.com 2019 - 2024. All rights reserved.