cipher-base icon indicating copy to clipboard operation
cipher-base copied to clipboard

Module not found: Error: Can't resolve 'stream'

Open abdulmanan7 opened this issue 7 years ago • 19 comments

I am using angular 6 when i run ng serve this error pops

ERROR in ./node_modules/cipher-base/index.js Module not found: Error: Can't resolve 'stream' in '/home/devspot3/Sites/d-poll/node_modules/cipher-base' ERROR in ./node_modules/hash-base/index.js Module not found: Error: Can't resolve 'stream' in '/home/devspot3/Sites/d-poll/node_modules/hash-base'

abdulmanan7 avatar Sep 12 '18 06:09 abdulmanan7

what are you using to bundle the module and do you have some non-default settings related to node built in modules?

calvinmetcalf avatar Sep 12 '18 12:09 calvinmetcalf

@calvinmetcalf angular-cli disables node packages in webpack config and does not have an option for enabling

artaommahe avatar Nov 10 '18 14:11 artaommahe

@artaommahe this sounds like It might be a problem with angular-cli then

calvinmetcalf avatar Nov 12 '18 13:11 calvinmetcalf

@calvinmetcalf it's an issue with any build pipeline that does not use default node packages. We can use universal versions instead to avoid such problems https://github.com/webpack/node-libs-browser

artaommahe avatar Nov 12 '18 14:11 artaommahe

this was written to be part of node-libs-browser so it assumes that those are already present, if it didn't then people who used node-libs-package might get multiple versions of dependencies bundled into the app which is something we'd like to avoid.

calvinmetcalf avatar Nov 12 '18 15:11 calvinmetcalf

If anyone's still having issues with this, you can fix it in your Angular project using tsconfig paths to point to a browser-compatible stream lib:

  "compilerOptions": {
    "paths": {
      "stream": ["./node_modules/readable-stream"]
    }
  },

kyranjamie avatar Jun 11 '20 08:06 kyranjamie

The above didn't help for our react native project built with React. After adding solution from kyranjamie we still get the error.

devdomsos avatar Jul 19 '21 13:07 devdomsos

patch-package helped me: npm i patch-package

in the package.json add this line:

"scripts": {
  "postinstall": "patch-package",
}

opend the problem file and correct it. In my case: node_modules/cipher-base/index.js

var Buffer = require('safe-buffer').Buffer
var Transform = require('readable-stream').Transform // replacing instead of "stream"
var StringDecoder = require('string_decoder').StringDecoder
var inherits = require('inherits')

function CipherBase (hashMode) {
...

run the command from a root dir of your probject: npx patch-package cipher-base

it'll create a new folder patches in the root dir and add there this fix. That's all. Commit changes. It'll automaticaly replace code in the node_modules after reinstalling packages

Disti4ct avatar Aug 02 '21 09:08 Disti4ct

Merci j'avais un problème avec CipherBase sur mon projet j'ai modifier le package.json .. "scripts": { "postinstall": "patch-package", } puis j'ai installée le paquet via npx patch-package cipher-base. "le problème est resolu"

santosney avatar Sep 09 '21 16:09 santosney

patch-package helped me: npm i patch-package

in the package.json add this line:

"scripts": {
  "postinstall": "patch-package",
}

opend the problem file and correct it. In my case: node_modules/cipher-base/index.js

var Buffer = require('safe-buffer').Buffer
var Transform = require('readable-stream').Transform // replacing instead of "stream"
var StringDecoder = require('string_decoder').StringDecoder
var inherits = require('inherits')

function CipherBase (hashMode) {
...

run the command from a root dir of your probject: npx patch-package cipher-base

it'll create a new folder patches in the root dir and add there this fix. That's all. Commit changes. It'll automaticaly replace code in the node_modules after reinstalling packages

Perfect solution

dabuchera avatar Oct 27 '21 21:10 dabuchera

Is this a general issue with the package or does it only occur in specific situations? I would prefer having an upstream rather than patching up the package in production.

networkException avatar Nov 17 '21 20:11 networkException

I have the same issue with webpack 5, when using pollyfils like this:

webpackConfig.resolve.fallback = {
    fs: false,                          
    crypto: require.resolve("crypto-browserify"),
    stream: require.resolve("stream-browserify")
}

I get the following error message:

index.js:11 Uncaught TypeError: Cannot read properties of undefined (reading 'call')
    at Hash.CipherBase (index.js:11)

pszalko avatar Nov 22 '21 15:11 pszalko

patch-package helped me: npm i patch-package

in the package.json add this line:

"scripts": {
  "postinstall": "patch-package",
}

opend the problem file and correct it. In my case: node_modules/cipher-base/index.js

var Buffer = require('safe-buffer').Buffer
var Transform = require('readable-stream').Transform // replacing instead of "stream"
var StringDecoder = require('string_decoder').StringDecoder
var inherits = require('inherits')

function CipherBase (hashMode) {
...

run the command from a root dir of your probject: npx patch-package cipher-base

it'll create a new folder patches in the root dir and add there this fix. That's all. Commit changes. It'll automaticaly replace code in the node_modules after reinstalling packages

perfect!!!

LuisFernandoPeretti avatar Jan 14 '22 01:01 LuisFernandoPeretti

if anyone's wandered here from react/ts (me) downgrading react-scripts to 4.0.3 is the only thing that has resolved this issue

  1. update react-scripts version to 4.0.3 in your package.json
  2. rm yarn.lock package.lock; rm -rf node_modules
  3. yarn install or npm install again and restart

remxx avatar Mar 02 '22 20:03 remxx

@remxx suggestion fixed it for me 🥳 !

if anyone's wandered here from react/ts (me) downgrading react-scripts to 4.0.3 is the only thing that has resolved this issue

  1. update react-scripts version to 4.0.3 in your package.json
  2. rm yarn.lock package.lock; rm -rf node_modules
  3. yarn install or npm install again and restart

The issue for me stemmed from installing web3js. I believe by downgrading the scripts you are switching to Webpack 4 which poly fills some node core libraries.

owencraston avatar Mar 03 '22 23:03 owencraston

npm i stream worked for me

polarisiota avatar Mar 19 '22 06:03 polarisiota

npm i stream worked for me

This works fine on the latest versions on React!

lionpunk avatar Apr 02 '22 21:04 lionpunk

while a node module exposed to web we are in trouble

ERROR in ./node_modules/cipher-base/index.js 2:16-43
Module not found: Error: Can't resolve 'stream' in '/Users/masoudsoroush/Sites/lisk-desktop/node_modules/cipher-base'

here is how we can transform a package from web pack to use alternative packages

Fallback

https://webpack.js.org/configuration/resolve/#resolvefallback

module.exports = {
  // ...the rest of your config
  resolve: {
    fallback: {
    stream: require.resolve('stream-browserify'),
    }
  }
}

Alias

if its not work for you, you can alias the packages

// webpack.config.js
module.exports = {
  // ...the rest of your config

  resolve: {
    alias: {
      'stream$': 'stream-browserify'
    }
  }
}

@calvinmetcalf here is how you can export packages for browser, thank you for you amazing job <3 https://webpack.js.org/guides/package-exports

soroushm avatar Oct 21 '22 13:10 soroushm