Brain.js: Can't resolve 'stream'

Created on 27 Dec 2018  路  8Comments  路  Source: BrainJS/brain.js

What is wrong?

Installing brain.js via npm in an angular framework doesn't work.
Error:

ERROR in ./node_modules/brain.js/dist/train-stream.js
Module not found: Error: Can't resolve 'stream' in '../node_modules/brain.js/dist'

Where does it happen?

When running npm start

How do we replicate the issue?

  • Fresh Project with Angular 7.1.4
  • npm install brain.js
  • npm start

How important is this (1-5)?

5

Expected behavior (i.e. solution)

Brain.js shouldn't throw this error

Other Comments

Most helpful comment

Ran into the same issue, got it temporarily fixed by adding the following to my index.html :

<script> var global = global || window; var Buffer = Buffer || []; var process = process || { env: { DEBUG: undefined }, version: [] }; </script>

https://stackoverflow.com/questions/50356408/upgrading-to-angular-6-x-gives-uncaught-referenceerror-global-is-not-defined

All 8 comments

What version of node?

tried with node v10.4.1 and v.11.6.0
both are throwing this error

This is apparently a problem with angular's build system: https://github.com/DevExpress/devextreme-angular/issues/776

Very odd I might add. I would like to find a solution, besides the existing "install an old package" one that seems to be the accepted norm.

Yeah saw that too.
Installing steam package leads to this error:

train-stream.js:15 Uncaught TypeError: Super expression must either be null or a function, not undefined
    at _inherits (train-stream.js:15)
    at train-stream.js:24
    at Object../node_modules/brain.js/dist/train-stream.js (train-stream.js:165)
    at __webpack_require__ (bootstrap:78)
    at Object../node_modules/brain.js/index.js (index.js:6)
    at __webpack_require__ (bootstrap:78)
    at Module../src/app/gender/gender.component.ts (main.js:284)
    at __webpack_require__ (bootstrap:78)
    at Module../src/app/app.module.ts (app.component.ts:8)
    at __webpack_require__ (bootstrap:78)
_inherits @ train-stream.js:15
(anonymous) @ train-stream.js:24
./node_modules/brain.js/dist/train-stream.js @ train-stream.js:165
__webpack_require__ @ bootstrap:78
./node_modules/brain.js/index.js @ index.js:6
__webpack_require__ @ bootstrap:78
./src/app/gender/gender.component.ts @ main.js:284
__webpack_require__ @ bootstrap:78
./src/app/app.module.ts @ app.component.ts:8
__webpack_require__ @ bootstrap:78
./src/main.ts @ main.ts:1
__webpack_require__ @ bootstrap:78
0 @ main.ts:12
__webpack_require__ @ bootstrap:78
checkDeferredModules @ bootstrap:45
webpackJsonpCallback @ bootstrap:32
(anonymous) @ main.js:1

That is what I get when I change require("stream") to require("readable-stream")

caught ReferenceError: process is not defined
    at Object../node_modules/process-nextick-args/index.js (index.js:3)
    at __webpack_require__ (bootstrap:78)
    at Object../node_modules/readable-stream/lib/_stream_readable.js (_stream_readable.js:26)
    at __webpack_require__ (bootstrap:78)
    at Object../node_modules/readable-stream/readable-browser.js (readable-browser.js:1)
    at __webpack_require__ (bootstrap:78)
    at Object../node_modules/brain.js/dist/train-stream.js (train-stream.js:10)
    at __webpack_require__ (bootstrap:78)
    at Object../node_modules/brain.js/index.js (index.js:6)
    at __webpack_require__ (bootstrap:78)
./node_modules/process-nextick-args/index.js @ index.js:3
__webpack_require__ @ bootstrap:78
./node_modules/readable-stream/lib/_stream_readable.js @ _stream_readable.js:26
__webpack_require__ @ bootstrap:78
./node_modules/readable-stream/readable-browser.js @ readable-browser.js:1
__webpack_require__ @ bootstrap:78
./node_modules/brain.js/dist/train-stream.js @ train-stream.js:10
__webpack_require__ @ bootstrap:78
./node_modules/brain.js/index.js @ index.js:6
__webpack_require__ @ bootstrap:78
./src/app/gender/gender.component.ts @ main.js:284
__webpack_require__ @ bootstrap:78
./src/app/app.module.ts @ app.component.ts:8
__webpack_require__ @ bootstrap:78
./src/main.ts @ main.ts:1
__webpack_require__ @ bootstrap:78
0 @ main.ts:12
__webpack_require__ @ bootstrap:78
checkDeferredModules @ bootstrap:45
webpackJsonpCallback @ bootstrap:32
(anonymous) @ main.js:1

Ran into the same issue, got it temporarily fixed by adding the following to my index.html :

<script> var global = global || window; var Buffer = Buffer || []; var process = process || { env: { DEBUG: undefined }, version: [] }; </script>

https://stackoverflow.com/questions/50356408/upgrading-to-angular-6-x-gives-uncaught-referenceerror-global-is-not-defined

Yep, that fixes the error.
Steps to fix:

  1. add the script what @jordyvanderhaegen wrotes
  2. change require("stream")to require("readable-stream") in node_modules/brain.js/dist/train-stream.js

It should work then.
I let this one open, maybe @robertleeplummerjr will find another solution.

reproduced, I'll try and have a work around shortly.

Add this to tsconfig.json (found https://github.com/Stuk/jszip/issues/524):

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

Add this to polyfill.ts (found partly https://github.com/algolia/algoliasearch-client-javascript/issues/691, and above, ty @jordyvanderhaegen):

const w = (window as any);
w.process = {
  env: { DEBUG: undefined },
};
w.Buffer = [];
w.process = { env: { DEBUG: undefined }, version: [] };
w.global = w;

I installed this locally and was able to have the app simply work.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

panzad3 picture panzad3  路  5Comments

shopsoy picture shopsoy  路  3Comments

tyt34 picture tyt34  路  5Comments

nezzard picture nezzard  路  5Comments

VEXLife picture VEXLife  路  4Comments