2

I have been trying to run scripts using NPM run scripts. However, it ends up in error.

Package.json

"scripts": {
    "ng": "ng",
    "start": "ng serve --open",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },

**Error while running in console**
> ng serve --open

'T\project-name\node_modules\.bin\' is not recognized as an internal or external command,
operable program or batch file.
internal/modules/cjs/loader.js:968
  throw err;
  ^

Error: Cannot find module 'E:\Projects\@angular\cli\bin\ng'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:965:15)
    at Function.Module._load (internal/modules/cjs/loader.js:841:27)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! project-name@0.0.0 start: `ng serve --open`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the project-name@0.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Noel\AppData\Roaming\npm-cache\_logs\2020-09-17T08_36_36_725Z-debug.log

E:\Projects\project-folder\project-name>npm run start

> project-name@0.0.0 start E:\Projects\project-folder\project-name
> ng serve --open

'T\project-name\node_modules\.bin\' is not recognized as an internal or external command,
operable program or batch file.
internal/modules/cjs/loader.js:968
  throw err;
  ^

Error: Cannot find module 'E:\Projects\@angular\cli\bin\ng'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:965:15)
    at Function.Module._load (internal/modules/cjs/loader.js:841:27)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! project-name@0.0.0 start: `ng serve --open`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the project-name@0.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\user name\AppData\Roaming\npm-cache\_logs\2020-09-17T08_36_39_450Z-debug.log

Error Log

0 info it worked if it ends with ok
1 verbose cli [
1 verbose cli   'C:\\Program Files\\nodejs\\node.exe',
1 verbose cli   'C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli   'run',
1 verbose cli   'start'
1 verbose cli ]
2 info using npm@6.14.6
3 info using node@v12.18.4
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
5 info lifecycle project-name@0.0.0~prestart: project-name@0.0.0
6 info lifecycle project-name@0.0.0~start: project-name@0.0.0
7 verbose lifecycle project-name@0.0.0~start: unsafe-perm in lifecycle true
8 verbose lifecycle project-name@0.0.0~start: PATH: C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin;E:\Projects\AT&T\at-t_parallon_fe\node_modules\.bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Git\cmd;C:\xampp\php;C:\ProgramData\ComposerSetup\bin;C:\Program Files\nodejs\;C:\Users\Noel\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\Noel\AppData\Roaming\Composer\vendor\bin;C:\Users\Noel\AppData\Roaming\npm
9 verbose lifecycle project-name@0.0.0~start: CWD: E:\Projects\AT&T\at-t_parallon_fe
10 silly lifecycle project-name@0.0.0~start: Args: [ '/d /s /c', 'ng serve --open' ]
11 silly lifecycle project-name@0.0.0~start: Returned: code: 1  signal: null
12 info lifecycle project-name@0.0.0~start: Failed to exec start script
13 verbose stack Error: project-name@0.0.0 start: `ng serve --open`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\index.js:332:16)
13 verbose stack     at EventEmitter.emit (events.js:315:20)
13 verbose stack     at ChildProcess.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\lib\spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:315:20)
13 verbose stack     at maybeClose (internal/child_process.js:1021:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:286:5)
14 verbose pkgid project-name@0.0.0
15 verbose cwd E:\Projects\project-folder\project-name
16 verbose Windows_NT 10.0.10240
17 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "start"
18 verbose node v12.18.4
19 verbose npm  v6.14.6
20 error code ELIFECYCLE
21 error errno 1
22 error project-name@0.0.0 start: `ng serve --open`
22 error Exit status 1
23 error Failed at the project-name@0.0.0 start script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

Resolutions tried

  • I tried reinstalling the node
  • I tried adding the system32 path to the PATH variable in the system variables.

But still, I am facing the issue. Any help with the issue will be greatly appreciated.

4 Answers4

2

Just confirming if you have installed all dependencies from package.json before starting the server?

If not, please do that before hand using npm install.

If already done, Try adding @angular/cli as global dependency. You can have it installed globally in your system using - npm i -g @angular/cli then run - npm start

Dharman
  • 30,962
  • 25
  • 85
  • 135
DIGVJSS
  • 463
  • 1
  • 10
  • 21
  • @Dharman I have already performed npm install, and it's not just angular CLI that is causing the problem. Whatever script I try to run is failing. However, If I try to run the same directly in the console it will work. – Noel sebastian Sep 18 '20 at 09:57
1

When installing npm also remember that you package-lock.json is keeping deeper dependencies. Sometimes is good to clean your npm dependencies. That is if your project worked before and after instlling somthing bad stooped to:

npm cache clean -f

Install you npm :

npm install 
npm i

That will install all packages from your package.json.
When npm intalled but not working I found that installing CLI deliberately helps. I mean even if it was already in package.json dependency:

npm i @angular/cli
or
npm i @angular/cli@<yourversion maybe lower then latest>

You may install globally. That means is installed in you system and not only in that specific project directory. You can have multiple directories with different projects in totally different Angular version. Remember though that one installed globally may interfere with all other projects. So you may want to install not latest version but one you used:

npm i -g @angular/cli
npm i -g @angular/cli@<yourVersion>

When starting remember to be INSIDE folder of your project. When starting with npm :

npm run start 
npm run <hereNameYouGaveInPackageJson>

When starting with Angular dev server:

    ng start -o   
    ng start --open
PPik
  • 11
  • 4
  • I have already tried all these methods and the angular CLI is already installed globally in my system. It's not just for angular CLI any script given in the script option of package.json is failing. – Noel sebastian Sep 18 '20 at 10:02
  • Noel please remove Node_Module folder. Delete file package-lock.json (NOT package.json!). Then run npm cache clean. Install npm again. Should help. What is not ok that NPM can not find proper ng . That indicates you have mess I your NODE-MODULE folder. Would be helpful if you would include your package.json file. – PPik Sep 20 '20 at 16:28
  • Plus. Installing with -g can even cause your issues. So you have to think before including some crazy third party code into your global path. I WOULD UNISYLTALL whole global patch npm. Then install clean after cache clean. Review your PATHs you could have added to OS. There have to be reason why your npm CAN NOt find directory with ng code. Error clearly states that. Go do your node-module folder and try to find what error said no ng in E:\Projects\@angular\cli\bin\ng – PPik Sep 20 '20 at 16:42
  • Go do your node-module folder and try to find what error said no ng in E:\Projects\@angular\cli\bin\ng. First of all why it is not looking I (your project dir)/Node_modules/@angular/cli/bin/ng ? Hmm? – PPik Sep 20 '20 at 16:49
  • I have also tried the above methods, the packages I have installed globally are angular-CLI, typescript, and eslint. And the issue is not just for ng for every command If I try to add them in the scripts of package.json and run, they won't work. At the same time FI I run the same command directly into the terminal it will work. – Noel sebastian Sep 24 '20 at 04:16
0

you might have to add @angular/cli as devDependency. Since, you are saying, you were able to run script commands directly but while running it as npm start.

As error stack trace mentions, it's not able to find @angular/cli.

Try: -D is short for adding devDependency.
npm install -D @angular/cli and then npm start.

Sagar More
  • 466
  • 2
  • 8
  • Its not just angular CLI that's causing the issue. I tried running node and lint. For every package when I try running it by adding it to the script tag in package.json its not working. At the same time If I try running it in the same project in the console its working. – Noel sebastian Sep 18 '20 at 10:01
0

Whenever you get the error of missing scripts, it means that the scripts do not exist in the folder from which you are trying to access them.

Try this instead.

Ensure that you are in the exact folder from which you are trying to run the scripts from. It could be that you have two similarly named folders and that you are trying to run the scripts from the one without the node modules or that you are just trying to run the scripts from a totally different folder that doesn't include the node modules.

Afoma
  • 1