如何解决npm错误"npm ERR! code ELIFECYCLE"

493

我正在尝试学习React,所以我有一个完整的全栈React投票应用程序的示例代码。我正在尝试让它工作,但是在运行npm install后跟着npm start之后,当我运行npm start时,我收到以下错误信息,请问有人能帮我解决这个问题吗:

npm ERR! Darwin 16.4.0
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "server"
npm ERR! node v7.5.0
npm ERR! npm  v4.3.0
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! voting_app@1.1.0 server: `live-server --public --    
host=localhost --port=3000 --middleware=./disable-browser-cache.js`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the voting_app@1.1.0 server script 'live-server --
public --host=localhost --port=3000 --middleware=./disable-browser- 
cache.js'.
npm ERR! Make sure you have the latest version of node.js and npm  
installed.
npm ERR! If you do, this is most likely a problem with the voting_app  
package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     live-server --public --host=localhost --port=3000 --  
middleware=./disable-browser-cache.js
npm ERR! You can get information on how to open an issue for this  
project with:
npm ERR!     npm bugs voting_app
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls voting_app
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /Users/ItsMeMrLi/.npm/_logs/2017-02-17T22_48_03_581Z-
debug.log

npm ERR! Darwin 16.4.0
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "start"
npm ERR! node v7.5.0
npm ERR! npm  v4.3.0
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! voting_app@1.1.0 start: `npm run server`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the voting_app@1.1.0 start script 'npm run server'.
npm ERR! Make sure you have the latest version of node.js and npm 
installed.
npm ERR! If you do, this is most likely a problem with the voting_app    
package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     npm run server

npm ERR! You can get information on how to open an issue for this   
project with:
npm ERR!     npm bugs voting_app
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls voting_app
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /Users/ItsMeMrLi/.npm/_logs/2017-02-17T22_48_03_655Z-
debug.log

以下是我的 package.json 文件:

{
  "name": "voting_app",
  "version": "1.1.0",
  "author": "Fullstack.io",
  "scripts": {
    "go": "open http://localhost:3000; npm run server",
    "e2e": "nightwatch",
    "test": "./node_modules/.bin/concurrently -k 'npm run server' 'npm  
run e2e'",
    "start": "npm run server",
    "server": "live-server public --host=localhost --port=3000 --  
middleware=./disable-browser-cache.js"
  },
  "private": true,
  "devDependencies": {
  "concurrently": "2.2.0",
  "live-server": "git://github.com/acco/live-server.git"
},
  "dependencies": {
  "semantic-ui": "git://github.com/Semantic-Org/Semantic-
  UI.git#27d58a01793b66318478fbc5b6676804d22d065d"
  }
}

最后附上我的日志文件:
0 info it worked if it ends with ok
1 verbose cli [ '/usr/local/bin/node', '/usr/local/bin/npm', 'start' ]
2 info using npm@4.3.0
3 info using node@v7.5.0
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
5 info lifecycle voting_app@1.1.0~prestart: voting_app@1.1.0
6 silly lifecycle voting_app@1.1.0~prestart: no script for prestart, continuing
7 info lifecycle voting_app@1.1.0~start: voting_app@1.1.0
8 verbose lifecycle voting_app@1.1.0~start: unsafe-perm in lifecycle true
9 verbose lifecycle voting_app@1.1.0~start: PATH: /usr/local/lib/node_modules/npm/bin/node-gyp-bin:/Users/ItsMeMrLi/Downloads/fullstack-react-code/voting_app/node_modules/.bin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/Users/ItsMeMrLi/.rvm/gems/ruby-2.3.1/bin:/Users/ItsMeMrLi/.rvm/gems/ruby-2.3.1@global/bin:/Users/ItsMeMrLi/.rvm/rubies/ruby-2.3.1/bin:/Users/ItsMeMrLi/.cargo/bin:/usr/local/Cellar/smlnj/110.74/libexec/bin:/usr/local/bin:/Users/ItsMeMrLi/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Postgres.app/Contents/Versions/latest/bin:/Downloads/geckodriver:/usr/local/bin:/Users/ItsMeMrLi/.rvm/bin
10 verbose lifecycle voting_app@1.1.0~start: CWD: /Users/ItsMeMrLi/Downloads/fullstack-react-code/voting_app
11 silly lifecycle voting_app@1.1.0~start: Args: [ '-c', 'npm run server' ]
12 silly lifecycle voting_app@1.1.0~start: Returned: code: 1  signal: null
13 info lifecycle voting_app@1.1.0~start: Failed to exec start script
14 verbose stack Error: voting_app@1.1.0 start: `npm run server`
14 verbose stack Exit status 1
14 verbose stack     at EventEmitter.<anonymous> (/usr/local/lib/node_modules/npm/lib/utils/lifecycle.js:279:16)
14 verbose stack     at emitTwo (events.js:106:13)
14 verbose stack     at EventEmitter.emit (events.js:192:7)
14 verbose stack     at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/lib/utils/spawn.js:40:14)
14 verbose stack     at emitTwo (events.js:106:13)
14 verbose stack     at ChildProcess.emit (events.js:192:7)
14 verbose stack     at maybeClose (internal/child_process.js:890:16)
14 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5)
15 verbose pkgid voting_app@1.1.0
16 verbose cwd /Users/ItsMeMrLi/Downloads/fullstack-react-code/voting_app
17 error Darwin 16.4.0
18 error argv "/usr/local/bin/node" "/usr/local/bin/npm" "start"
19 error node v7.5.0
20 error npm  v4.3.0
21 error code ELIFECYCLE
22 error errno 1
23 error voting_app@1.1.0 start: `npm run server`
23 error Exit status 1
24 error Failed at the voting_app@1.1.0 start script 'npm run server'.
24 error Make sure you have the latest version of node.js and npm installed.
24 error If you do, this is most likely a problem with the voting_app package,
24 error not with npm itself.
24 error Tell the author that this fails on your system:
24 error     npm run server
24 error You can get information on how to open an issue for this project with:
24 error     npm bugs voting_app
24 error Or if that isn't available, you can get their info via:
24 error     npm owner ls voting_app
24 error There is likely additional logging output above.
25 verbose exit [ 1, true ]

2
请确保驱动器上还有剩余空间。 - Mircea
如果你和我一样,正在跟随《FullStack React》这本书。当你发布这个问题时,很可能你省略了在你发布之前的信息中至关重要的部分。请参见此处 https://dev59.com/X2ox5IYBdhLWcg3wtmc1#65399700(禁用浏览器缓存.js的相对路径)。`./` -> ../.. https://github.com/tapio/live-server/issues/245 - JGFMK
50个回答

548
第一步:使用$ npm cache clean --force命令清除缓存。
第二步:通过$ rm -rf node_modules(在Windows中使用rmdir /S /Q node_modules)命令删除node_modules文件夹,或者手动进入目录,右键单击>删除/移动到回收站来删除它。如果您不打算更新软件包,也可以删除package-lock.json文件。
第三步:npm install命令。
重新开始,使用$ npm start命令。
这对我有效,希望对您也有效。
虽然上述建议重新创建package-lock.json文件,但请确保package-lock.json文件首先存在,并且被版本控制系统/VCS(如果项目有VCS)跟踪。
附注:如果问题仍然存在,请检查显示为红色的错误并相应地采取措施。此错误特定于node.js环境。

40
使用 rm -rf 命令时要小心。 - Suhayb
39
在 Windows 中的第二步,您可以在 cmd 中执行 rd /s /q "node_modules" 或在 PowerShell 中执行 rm -r "node_modules"来源 - Travis Heeter
2
在我的情况下,删除 node_modules 文件夹有所帮助,但是实际上是项目的上一级文件夹。而执行 npm cache clean --force 命令会导致奇怪的行为并且会产生误导。在删除该目录之前,我还删除了本地存储库,并重新克隆了它,不确定这是否有所帮助,因为我所描述的操作最终解决了问题。 - Lukasz
1
对于第二步,我认为你可以使用npm ci。 - vuquanghoang
第二步的另一个选项是 npx rimraf node_modules - Stuart Hallows
显示剩余2条评论

163

仅清理CacheNode_module不够,按照以下步骤操作:

  • npm cache clean --force
  • 删除 node_modules 文件夹
  • 删除 package-lock.json 文件
  • npm install

按照这种方式对我有效。


24
我使用 npm cache verify 命令来修复问题,而不是删除整个缓存。 - Jared
5
我认为最重要的是删除 package-lock.json 文件,在尝试此操作之前,我已经尝试了许多方法都不能解决这个错误。 - BobGao
49
仅通过删除自动生成的构建部件就可以修复任何问题,这正是JS领域存在问题的体现... 如果不是因为这个stackoverflow条目,我会因为在构建日志中找不到可行的错误而束手无策! - pmarreck
@Jared,npm cache verify 是替换了所有四个步骤还是只替换了第一个步骤? - Daniel Kaplan
不要手动修改 package-lock.json 文件。相反,删除它并运行 npm update。这样做可能会导致问题。 - Adrian P.
显示剩余2条评论

43

我解决了运行以下代码时出现的错误

npm cache clean

然后手动从我的项目结构中删除node_modules目录,或者使用以下命令进行删除

rm -rf node_modules

然后再次使用以下命令安装依赖项

npm install

10
如果你能为像我这样的初学者详细解释答案的话,会非常有帮助。简洁明了固然重要,但并不总是有帮助的。谢谢。 - Mr. Unnormalized Posterior
1
npm run clean 不是一个命令。但是,你的想法还是有效的。我删除并重新安装了它...然后它就开始工作了...谢谢。 - Tarunn
在我类似的情况下,只有最后一条指令“npm install”有效。 - ged
在将我的代码从桌面复制/粘贴到服务器后,我遇到了Nuxt.js的问题。现在它已经解决了,谢谢。 - bill.gates
npm 清除缓存然后 npm 验证缓存然后 rm -rf node_modules 然后 npm 安装 - Winnie Nyambura

34

首先我运行了:

npm run clean

虽然有错误,但是我仍然继续。
然后,我删除了 node_modules 文件夹并运行。
npm install

这似乎已经解决了问题。


33

当我在一台16.04的DigitalOcean Ubuntu实例上运行使用create-react-app创建的应用程序时(链接),在运行npm run build命令时,出现了类似的错误提示。我将实例从512MB RAM升级到1GB(从$5/mo到$10/mo),然后脚本就可以运行了。

我在这里发布此贴是为了指出,您可能会因为资源限制而遇到此错误,而我在issue页面和SO答案中并没有看到这方面的解释。而且我在错误日志中也没有看到任何指示。


2
你是完全正确的!没有任何东西指向我这个方向,这是由于资源限制导致的一个错误...谢谢。 - Kubadev
1
是的,我在 EC2 t3a.nano 实例上编译 Laravel-mix 时遇到了类似的问题。看起来是内存不足导致的。在更大的实例上解决了这个问题。 - mCpXZKq3ruqgFXrvvAWRj9cYxk8RLX
在执行此操作之前,您可能需要尝试先重新启动服务器:https://www.digitalocean.com/community/questions/how-to-reboot-droplet-through-the-command-line-restart-apache-server对我来说,这已经足够了 :) - jumelet

22

更改 node_modules 目录中的访问权限

chmod -R a+rwx ./node_modules 

1
如果@pacanga,那么权限已正确设置,请参考其他解决方案。 - Taher

20

这个解决方案适用于Windows用户。

您可以打开node.js安装程序,让安装程序花些时间计算空间需求,然后点击下一步和删除按钮。这将从您的计算机中删除node.js,再次打开安装程序并将其安装在此路径中 - C:\Windows\System32

或者

清理缓存(Cache)Node_module将会奏效。

按照以下步骤操作:

  • npm cache clean --force
  • 删除node_modules文件夹
  • 删除package-lock.json文件
  • npm install

最近我在Windows上将我的Node更新到了12.X.X版本,但是我仍然看到相同的错误。清除缓存并没有帮助。还有其他可能的解决方案吗? - chris
@chris 你尝试在我回答中提到的节点路径重新安装了吗? - user13769010
@chris 第一次修复的目的不是更新,而是重新安装在路径 C:\Windows\System32 中。 - user13769010

13

遇到这个问题时,

对我而言,通过删除package-lock.json文件并重新运行npm install命令解决了问题。

如果上述方法无效,请尝试以下步骤:

  1. 删除package-lock.json文件
  2. npm cache clean --force
  3. npm install
  4. npm start

11

我在运行构建时遇到了 npm ERR! code ELIFECYCLE 错误。清除缓存并删除 package-lock.json 文件没有修复它。

在我的情况下,有一个正在 Docker 镜像上运行的 Angular 构建导致了这个问题。根本原因是在 MacOS 上在 Docker 中运行构建,它在一个 Linux 虚拟机中运行 Docker,并默认分配了 2GB 内存。

为 Docker 分配更多内存解决了我的问题。不得不说,错误信息应该更加清晰明了。

对我有帮助的答案是“Call retries were exceeded exception while ng build”


11

删除 node_modules 和 package-lock.json 文件,然后运行 npm install 命令。在这里我已经成功运行了(请在项目根目录下执行以下命令):

```npm 命令代码 ```
rm -rf node_modules && rm ./package-lock.json && npm install

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接