amplify-js: Unable to resolve module `util` and Can't find variable: Buffer and Unable to resolve module ‘stream’

First, I had the error, “Unable to resolve module util”. So I ran npm install util which solved the problem. Then I have the error, “Can’t find variable: Buffer”.

If I don’t import Amplify, then No error occurs. It is a brand new project.

"dependencies": {
"aws-amplify": "^0.4.0",
"aws-amplify-react-native": "^0.2.11",
"react": "16.2.0",
"react-native": "0.52.0",
}

I tried “aws-amplify”: and 0.1.0 and “aws-amplify-react-native”: “^0.2.11”,

here is my react-native info:

Environment:
  OS: macOS High Sierra 10.13.4
  Node: 10.0.0
  Yarn: 1.6.0
  npm: 5.6.0
  Watchman: 4.9.0
  Xcode: Xcode 9.3 Build version 9E145

Packages: (wanted => installed)
  react: 16.3.1 => 16.3.1
  react-native: 0.55.4

I have follow this solution > https://github.com/aws/aws-amplify/issues/140 but instead with :

"devDependencies": {
...,
    "rn-nodeify": "github:tradle/rn-nodeify"
}

It is worked but dependencies became a mess and also some warnings are showing on debug:

Module RCTImageLoader requires main queue setup since it overrides init but doesn’t implement requiresMainQueueSetup. In a future release React Native will default to initializing all native modules on a background thread unless explicitly opted-out of.

Module RNRandomBytes requires main queue setup since it overrides constantsToExport but doesn’t implement requiresMainQueueSetup. In a future release React Native will default to initializing all native modules on a background thread unless explicitly opted-out of.

Once I connect it to AWS, got error: Unable to resolve module ‘stream’

Thanks in advance

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 6
  • Comments: 15 (4 by maintainers)

Most upvoted comments

@rhishikeshj you can do a npm i util buffer then add this line in the lib/index.js of the aws-amplify package:

global.Buffer = global.Buffer || require('buffer').Buffer

You will be able to at least launch your app for debugging untill this issue is resolved

I solved this problem!

add package.json

"dependencies": {
    "amazon-cognito-identity-js": "2.0.3",
   ...
}

@vincen7su I managed to work around the “Can’t find variable: crypto” error by adding the following to the top of my App.js. It’s a bit of a hack though… looking forward to a proper fix!

global.crypto = require('crypto-browserify');
global.crypto.getRandomValues = (byteArray) => {
  for (let i = 0; i < byteArray.length; i++) {
    byteArray[i] = Math.floor(256 * Math.random());
  }
};

I am also having issues with the CRNA and aws-amplify in a similar environment using expo. I ran the link commands but for some reason I still get an error where the util package is required in the sha.js package

The similar issue results in this error:

The package at ".<removed>\\node_modules\\sha.js\\sha1.js" attempted to import the Node standard library module "util". It failed because React Native does not include the Node standard library. Read more at https://docs.expo.io/versions/latest/introduction/faq.html#can-i-use-nodejs-packages-with-expo
Environment:
  OS: Windows 10
  Node: 8.11.1
  Yarn: Not Found
  npm: 6.0.1
  Watchman: Not Found
  Xcode: N/A
  Android Studio: Version  3.0.0.0 AI-171.4443003

Packages: (wanted => installed)
  react: 16.3.1 => 16.3.1
  react-native: ~0.55.2 => 0.55.4

expo version 54.0.0

Finally Running: “npm ls sha.js”

cream-city-decode@0.1.0 <removed>
`-- aws-amplify@0.4.0
  `-- amazon-cognito-identity-js@2.0.5
    `-- crypto-browserify@3.0.0
      `-- sha.js@2.1.6