angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

ng test fails Module not found: Error: Can't resolve 'fs'

Open TKul6 opened this issue 9 years ago • 32 comments

Bug Report or Feature Request (mark with an x)

- [X] bug report -> please search issues before submitting
- [ ] feature request

Versions.

Angular CLI: 1.5.0 Node: 9.0.0 OS: win32 x64 Angular: 5.0.0 ... animations, common, compiler, compiler-cli, core, forms ... http, language-service, platform-browser ... platform-browser-dynamic, router

@angular/cli: 1.5.0 @angular-devkit/build-optimizer: 0.0.32 @angular-devkit/core: 0.0.20 @angular-devkit/schematics: 0.0.35 @ngtools/json-schema: 1.1.0 @ngtools/webpack: 1.8.0 @schematics/angular: 0.1.0 typescript: 2.4.2 webpack: 3.8.1

Repro steps.

ng test

Omits the following error:

ERROR in ./node_modules/fs.realpath/index.js Module not found: Error: Can't resolve 'fs' in XXXX\node_modules\fs.realpath' @ ./node_modules/fs.realpath/index.js 8:9-22 @ ./node_modules/jasmine/node_modules/glob/sync.js @ ./node_modules/jasmine/node_modules/glob/glob.js @ ./node_modules/jasmine/lib/jasmine.js @ ./src/client/app/account/login/login.component.spec.ts @ ./src/client .spec.ts$ @ ./src/client/test.ts

ERROR in ./node_modules/fs.realpath/old.js Module not found: Error: Can't resolve 'fs' in 'XXXX\node_modules\fs.realpath' @ ./node_modules/fs.realpath/old.js 24:9-22 @ ./node_modules/fs.realpath/index.js @ ./node_modules/jasmine/node_modules/glob/sync.js @ ./node_modules/jasmine/node_modules/glob/glob.js @ ./node_modules/jasmine/lib/jasmine.js @ ./src/client/app/account/login/login.component.spec.ts @ ./src/client .spec.ts$ @ ./src/client/test.ts

ERROR in ./node_modules/jasmine/node_modules/glob/sync.js Module not found: Error: Can't resolve 'fs' in 'XXXX\node_modules\jasmine\node_modules\glob' @ ./node_modules/jasmine/node_modules/glob/sync.js 4:9-22 @ ./node_modules/jasmine/node_modules/glob/glob.js @ ./node_modules/jasmine/lib/jasmine.js @ ./src/client/app/account/login/login.component.spec.ts @ ./src/client .spec.ts$ @ ./src/client/test.ts

ERROR in ./node_modules/jasmine/node_modules/glob/glob.js Module not found: Error: Can't resolve 'fs' inXXXX\node_modules\jasmine\node_modules\glob' @ ./node_modules/jasmine/node_modules/glob/glob.js 43:9-22 @ ./node_modules/jasmine/lib/jasmine.js @ ./src/client/app/account/login/login.component.spec.ts @ ./src/client .spec.ts$ @ ./src/client/test.ts

ERROR in ./node_modules/jasmine/node_modules/jasmine-core/lib/jasmine-core.js Module not found: Error: Can't resolve 'fs' in 'XXXX\node_modules\jasmine\node_modules\jasmine-core\lib' @ ./node_modules/jasmine/node_modules/jasmine-core/lib/jasmine-core.js 5:9-22 @ ./node_modules/jasmine/lib/jasmine.js @ ./src/client/app/account/login/login.component.spec.ts @ ./src/client .spec.ts$ @ ./src/client/test.ts

Desired functionality.

Run the tests

Mention any other details that might be useful.

It happened when I upgraded from cli 1.47 to 1.5.0.

Jasmine packages: "jasmine": "^2.8.0", "jasmine-core": "~2.5.2", "jasmine-spec-reporter": "~3.2.0"

Karma packages:

"karma": "~1.5.0", "karma-chrome-launcher": "~2.0.0", "karma-cli": "~1.0.1", "karma-coverage-istanbul-reporter": "^1.0.0", "karma-jasmine": "~1.1.0", "karma-jasmine-html-reporter": "^0.2.2", "karma-junit-reporter": "^1.2.0", "karma-mocha": "^1.3.0", "karma-phantomjs-launcher": "^1.0.4",

TKul6 avatar Nov 05 '17 12:11 TKul6

Same here

Scipionh avatar Nov 06 '17 06:11 Scipionh

I've spent some time trying to debug this, and no matter what, if I update the @angular/cli package to 1.5.0, this happens to me. I thought maybe it was something in the karma.conf.js and compared my current project's config to a new project's config and it was nearly identical. Even after matching things up as much as possible and disabling some of our plugins this still happens.

derekaug avatar Nov 07 '17 22:11 derekaug

Any news on this issue ?

Scipionh avatar Nov 27 '17 07:11 Scipionh

I got the same problem with the jsonpath (npm: "jsonpath": "^1.0.0") library. ng serve and ng build --prod is working. ng test fails.

ERROR in ./node_modules/jsonpath/lib/grammar.js Module not found: Error: Can't resolve 'fs' in '/home/m500040/gitrepo/lvm-core-components-client-lib/node_modules/jsonpath/lib' @ ./node_modules/jsonpath/lib/grammar.js 2:9-22 @ ./node_modules/jsonpath/lib/parser.js @ ./node_modules/jsonpath/lib/index.js @ ./node_modules/jsonpath/index.js @ ./src/app/core/services/json-handler/json-handler.service.ts @ ./src/app/core/services/json-handler/json-handler.service.spec.ts @ ./src .spec.ts$ @ ./src/test.ts

lammers avatar Nov 27 '17 10:11 lammers

same problem here

adamlubek avatar Nov 27 '17 12:11 adamlubek

for my little problem with the jsonpath library a workaround for the issue is using: import * as jsonPath from 'jsonpath/jsonpath'; instead of import * as jsonPath from 'jsonpath'; ... jsonPath.query(this._document, path || this.path);

lammers avatar Nov 27 '17 12:11 lammers

Same problem, any news?

michelre avatar Nov 29 '17 14:11 michelre

Hello guys same problem here any news ?

Stradivario avatar Nov 30 '17 15:11 Stradivario

same problem,need someone has been resolved

phamhaibkit avatar Dec 06 '17 03:12 phamhaibkit

I had the same error after running ng test that went away after removing import 'jasmine'; from *.spec.ts files.

If the editor is complaining about missing types for jasmine functions, you can fix that by editing tsconfig.spec.json file:

{
  "compilerOptions": {
    "types": [
      "jasmine",
      ...
    ]
  },
 ...
}

phong-kite avatar Dec 06 '17 07:12 phong-kite

Same problem here !! Any news ??

RadouaneRoufid avatar Dec 11 '17 10:12 RadouaneRoufid

This went away for me after updating to [email protected] & [email protected] and following these steps:

remove node_modules
remove package-lock.json
npm cache clean -f

(thanks to @istiti for the comment on #7966).

kevinkuszyk avatar Dec 12 '17 15:12 kevinkuszyk

thanks @kevinkuszyk, 😢 didn't work for me. Still get multiple "Can't resolve 'fs' in...." with [email protected].

SearchingSoul avatar Dec 13 '17 05:12 SearchingSoul

I had a "import 'jasmine'" in one of my tests. Removing it solved the problem.

RadouaneRoufid avatar Dec 13 '17 05:12 RadouaneRoufid

I believe a stray import 'jasmine'; was my issue as well.

derekaug avatar Dec 15 '17 17:12 derekaug

I have no "import 'jasmine'" in my tests 😒. tsconfig file is like @phong-kite's comment above.

Perhaps the issue is still open & marked as broken/urgent so probably need to wait..? 🤔

SearchingSoul avatar Dec 16 '17 11:12 SearchingSoul

After upgrading to angular 5.0.1 and the cli to 1.6 And removing the import 'jasmine' everything is working fine.

TKul6 avatar Dec 18 '17 17:12 TKul6

What worked for me is adding the following to karma.conf.js

webpack: { node: { fs: 'empty', } }

Hope it helps someone.

SearchingSoul avatar Dec 20 '17 06:12 SearchingSoul

@SearchingSoul ,I would like to sincerely thank you for your help.It's work for me

phamhaibkit avatar Dec 26 '17 07:12 phamhaibkit

Here, with Angular 5.2, Angular CLI 1.6.4, with no reference to import 'jasmine', I had to modify the Karma configuration as described here-before.

Cleaning caches and reinstalling NPM modules did not help.

Arnaud73 avatar Jan 15 '18 17:01 Arnaud73