7

I am having some troubles with an Angular Project. I am just learning Angular and encountering all sort of errors along the way.

My project is compiling and running fine but whenever I am trying to run an "ng test" to see if my test succeed I am getting all sort of errors back. They look like this:

ERROR in ./node_modules/webdriver-js-extender/node_modules/selenium-webdriver/firefox/extension.js
Module not found: Error: Can't resolve 'fs' in '

or

ERROR in ./node_modules/tough-cookie/lib/cookie.js
Module not found: Error: Can't resolve 'net' in 

And so on. I read about the fs that I was supposed to add target: 'node' in the webpack config. When I did an ng Eject the webpack config file had this

...
"node": {
    "fs": "empty",
...

My test looks something like this, I'm only attaching the part where errors have started:

it('should send credentials on submit', () => {
  let fixture = TestBed.createComponent(LoginComponent);
  let component: LoginComponent = fixture.componentInstance;
  let element = fixture.nativeElement;

  fixture.detectChanges();

  element.username = expectedUsername;


  element.query(by.id('login-username')).value = expectedUsername;
  element.query(by.id('login-username')).dispatchEvent(new Event('input'));
  element.query(by.id('login-password')).value = expectedPassword;
  element.query(by.id('login-password')).dispatchEvent(new Event('input'));
Vlad N
  • 631
  • 2
  • 10
  • 21
  • 3
    Please create a git repo and share the same. Its hard to just comment when you have already tried two possible solution – Tarun Lalwani Apr 16 '18 at 13:09
  • 1
    Have you tried running ng test with source maps off? That helps me sometimes. ng-test -sm=false I found it on an answer to [this question](https://stackoverflow.com/q/45399079/2391959) – josavish Apr 16 '18 at 20:33

2 Answers2

4

Two potential possibilities that I see:

  1. You may be importing a module incorrectly, this is very easy to do with IDEs that auto import for you. See the last comment on this thread for an example: https://github.com/angular/angular-cli/issues/8357

  2. The fs module is not supported in Angular (see the link bellow) since it requires the use of native code not available to the browser. In the code you provided you are not using fs. Is it possible you have imported it somewhere in your project? If you haven't directly imported it, it is possible that one of your libraries has. You may want to review the libraries you have npm install'ed for ones that don't support the browser. https://github.com/angular/angular-cli/issues/8272

As the comments on your post suggest, if you add more code (or link to a GitHub repository) we may be able to offer additional suggestions.

Pearman
  • 1,068
  • 5
  • 14
  • What I could tell is that the fs is coming from protactor from testing ... Apparently it's being used when I am using the by method to select an element. (I'm using material components) – Vlad N Apr 19 '18 at 10:52
1

You have to just delete the node modules folder inside project and run command 'npm install'. and check whether your problem will be solved or not..

Omkar Jadhav
  • 656
  • 1
  • 5
  • 18