我一直在尝试使用NPM运行脚本运行脚本。但是,它最终会出错。
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
错误日志
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 ]
尝试的解决方法
但是,我仍然面临这个问题。对于该问题的任何帮助将不胜感激。
答案 0 :(得分:1)
仅在启动服务器之前确认是否已安装package.json
中的所有依赖项?
如果没有,请先使用npm install
进行操作。
如果已经完成,
尝试添加@angular/cli
作为全局依赖项。您可以使用-npm i -g @angular/cli
将其全局安装在系统中
然后运行-npm start
答案 1 :(得分:0)
您可能必须添加industry
作为devDependency。因为您说的是,您可以直接运行脚本命令,但可以以@angular/cli
的形式运行。
正如错误堆栈跟踪所提到的,它无法找到npm start
。
尝试:
@angular/cli
是添加devDependency的缩写。
-D
,然后npm install -D @angular/cli
。
答案 2 :(得分:0)
在安装npm时,还请记住,package-lock.json保持了更深的依赖性。有时候清理npm依赖关系是一件好事。也就是说,如果您的项目在将某些东西放入其中之前和之后都可以正常工作,
npm cache clean -f
为您安装npm:
npm install
npm i
这将安装package.json中的所有软件包。
当npm安装但无法正常工作时,我发现故意安装CLI会有所帮助。我的意思是,即使它已经在package.json依赖项中:
npm i @angular/cli
or
npm i @angular/cli@<yourversion maybe lower then latest>
您可以全局安装。这意味着将安装在您的系统中,而不仅安装在该特定项目目录中。您可以在完全不同的Angular版本中具有多个包含不同项目的目录。请记住,尽管全局安装可能会干扰所有其他项目。因此,您可能要安装的不是最新版本,而是您使用的版本:
npm i -g @angular/cli
npm i -g @angular/cli@<yourVersion>
启动时,请记住要成为项目的INSIDE文件夹。 以npm开头时:
npm run start
npm run <hereNameYouGaveInPackageJson>
从Angular开发服务器启动时:
ng start -o
ng start --open