aws-sdk-js-v3: React-native | CognitoIdentityProviderClient | Unable to resolve module stream
Checkboxes for prior research
- I’ve gone through Developer Guide and API reference
- I’ve checked AWS Forums and StackOverflow.
- I’ve searched for previous similar issues and didn’t find any solution.
Describe the bug
Yesterday I updated my react-native app to version 0.72.0.
Since then I get issues that modules can’t be resolved when I try to run the Android emulator.
SDK version number
@aws-sdk/client-cognito-identity-provider@3.358.0
Which JavaScript Runtime is this issue in?
React Native
Details of the browser/Node.js/ReactNative version
0.72.0
Reproduction Steps
Reproduction steps
- Install react native & aws sdk:
npx react-native@latest init Project --template react-native-template-typescript
npm install @aws-sdk/client-cognito-identity-provider
- Add the following to the import section of to App.tsx:
import {
CognitoIdentityProviderClient,
InitiateAuthCommandInput,
InitiateAuthCommand,
AuthFlowType
} from "@aws-sdk/client-cognito-identity-provider";
const region = "us-east-1";
const cognitoClient = new CognitoIdentityProviderClient({
region,
});
- Add the following code to the App function:
const params: InitiateAuthCommandInput = {
ClientId: "clientId",
AuthFlow: AuthFlowType.USER_PASSWORD_AUTH,
AuthParameters: {
USERNAME: "username",
PASSWORD: "password",
},
};
const initiateAuthCommand = new InitiateAuthCommand(params);
cognitoClient.send(initiateAuthCommand)
- Then run the following commands:
npm start
npm run android
Observed Behavior
App does no longer launch. It gets the following error message:
Expected Behavior
App launches w/o error message.
Possible Solution
The simple solution seems to install the “stream” package. But then another module can’t be loaded. Until it says it can’t load the module “http”. And then I didn’t know what to do anymore.
Note that one month ago I didn’t have any of those issues.
Additional Information/Context
Another user with a similar issue: https://github.com/aws/aws-sdk-js-v3/issues/3549
Unfortunately he didn’t say how he solved his issue.
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 2
- Comments: 50 (6 by maintainers)
My prior post:
I have looked into the metro bundler to troubleshoot your issue. Here is what I found that works:
In the S3 Client package.json file, the following entry points are defined:
When using bundlers, the AWS SDK expects that you resolve the
module
(i.e. ESM) entry point, and as such the replacement directives are for files in thedist-es
distribution. In many of the errors shown above, users have resolved themain
field with thecjs
distribution.If your bundler is metro, it provides a configurable option called
config.resolver.resolverMainFields
You should modify this to include themodule
main field a.k.a. entry point.example:
If you subsequently encounter a syntax error around this block:
Add the plugin
@babel/plugin-proposal-export-namespace-from
to your babel config:After adding the
module
resolverMainField, my bundle using metro no longer attempted to access the Node.js modules such asstream
orhttps
. Again, do not attempt to polyfill those Node.js modules to get your application working, since they will not likely work as intended.in
node_modules/@smithy/util-stream/package.json
Edit line"main": "./dist-cjs/index.js"
to"main": "./dist-es/index.js"
Or patch-package@smithy+util-stream+1.0.1.patch
This is still an issue with the latest version.
Have similar problem with @aws-sdk/client-iot (version 3.360.0) in React Native project:
Android Bundling failed 3629ms The package at “node_modules@aws-sdk\client-iot\node_modules@aws-sdk\util-stream\dist-cjs\getAwsChunkedEncodingStream.js” attempted to import the Node standard library module “stream”. It failed because the native React runtime does not include the Node standard library.
I am having the same issue and i still get the same error after changing the sdk version. Edit: Ok so i got this to work with the help of @yenfryherrerafeliz. the version he provided
3.356
doesn’t exist. using this:3.354.0
version for the sdk worked for meAny updates on how to make
@smithy/node-http-handler
correctly work in react native? Still gettingThe package at "node_modules/@smithy/node-http-handler/dist-cjs/node-http2-handler.js" attempted to import the Node standard library module "http2".
Need to make it work ASAP…I tried that but it didn’t work. But what did for me was:
To further add onto this, I modified the @smithy/util-stream/package.json file and ran
yarn patch-package @smithy/util-stream --exclude
.Hi! Got a very similar issue with React Native and @aws-sdk/client-s3.
The package at "node_modules/@smithy/node-http-handler/dist-cjs/node-http2-handler.js" attempted to import the Node standard library module "http2".
Any help would be appreciated! My package.json:Thanks a lot. This is really helpful.
The bundler for React-Native projects is Metro. It seems that it doesn’t take into account this directive.
Could the issue here be related to the migration of the AWS javascript sdk to @smithy about one months ago?!