【问题标题】:npm run scripts not working, bur running scripts separately worksnpm 运行脚本不工作,bur 运行脚本单独工作
【发布时间】:2021-01-04 03:33:04
【问题描述】:

我一直在尝试使用 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 ]

已尝试的解决方法

  • 我尝试重新安装节点
  • 我尝试将system32路径添加到系统变量中的PATH变量中。

但是,我仍然面临着这个问题。任何有关此问题的帮助将不胜感激。

【问题讨论】:

    标签: node.js angular npm npm-scripts npm-start


    【解决方案1】:

    您可能需要添加 @angular/cli 作为 devDependency。因为,您是说,您可以直接运行脚本命令,但同时以npm start 身份运行。

    正如错误堆栈跟踪所提到的,它无法找到@angular/cli

    尝试: -D 是添加 devDependency 的缩写。
    npm install -D @angular/cli 然后是 npm start

    【讨论】:

    • 导致问题的不仅仅是角度 CLI。我尝试运行节点和 lint。对于每个包,当我尝试通过将其添加到 package.json 中的脚本标记来运行它时,它都不起作用。同时,如果我尝试在控制台的同一个项目中运行它,它的工作原理。
    【解决方案2】:

    只是在启动服务器之前确认你是否已经从package.json 安装了所有依赖项?

    如果没有,请事先使用npm install

    如果已经完成, 尝试添加 @angular/cli 作为全局依赖。您可以使用 -npm i -g @angular/cli 在系统中全局安装它 然后运行 ​​- npm start

    【讨论】:

    • @Dharman 我已经执行了 npm install,并且不仅仅是 angular CLI 导致了问题。无论我尝试运行什么脚本都失败了。但是,如果我尝试直接在控制台中运行它,它将起作用。
    【解决方案3】:

    在安装 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
    

    【讨论】:

    • 我已经尝试了所有这些方法,并且 Angular CLI 已经全局安装在我的系统中。 package.json 的 script 选项中给出的任何脚本都失败了。
    • Noel 请删除 Node_Module 文件夹。删除文件 package-lock.json(不是 package.json!)。然后运行 ​​npm cache clean。再次安装 npm。应该有帮助。 NPM 找不到合适的 ng 有什么不好的。这表明您的 NODE-MODULE 文件夹弄乱了。如果您包含您的 package.json 文件会很有帮助。
    • 加号。使用 -g 安装甚至会导致您的问题。因此,在将一些疯狂的第三方代码纳入您的全局路径之前,您必须三思而后行。我会统一整个全球补丁 npm。然后在缓存清理后安装清理。查看您可以添加到操作系统的 PATH。您的 npm 无法找到带有 ng 代码的目录一定是有原因的。错误清楚地说明了这一点。去做你的 node-module 文件夹并尝试在 E:\Projects\@angular\cli\bin\ng 中找到错误说 no ng
    • 去你的 node-module 文件夹并尝试在 E:\Projects\@angular\cli\bin\ng 中找到错误提示 no ng。首先为什么它不看 I (你的项目目录)/Node_modules/@angular/cli/bin/ng ?嗯?
    • 上述方法我也试过了,我全局安装的包是angular-CLI、typescript、eslint。而且问题不只是针对每个命令的 ng 如果我尝试将它们添加到 package.json 的脚本中并运行,它们将不起作用。同时 FI 我直接在终端中运行相同的命令它会工作。
    猜你喜欢
    • 1970-01-01
    • 2017-02-06
    • 2018-12-21
    • 2017-07-26
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    • 1970-01-01
    • 2017-03-09
    相关资源
    最近更新 更多