Fixes For: NPM err code elifecycle

Guide on getting rid of the error NPM err code elifecycle

Mar 6, 2023 | Read time 10 minutes

🔔 Table of contents

Introduction

So I was firing up a React tutorial on a recently provisioned virtual machine from work and got the error:

NPM err code elifecycle

This happened while I downloaded a React tutorial and started to run npm install. A more verbose error looks like something like the below:

  

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/<username>/.npm/_logs/2017-02-17T22_48_03_581Z-
debug.log

What does err code elifecycle mean?

The error code elifecycle just means that NPM encountered an error during executing a script that is linked to a lifecycle event?

Clear as mud right?

This error message is quite general, so we usually need to dig a bit deeper into the error log to find out what the cause is.

For example, in our original error log above, we can see the following three lines:

  
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn

We can see the error “npm ERR! errno ENOENT” - this means that NPM could not find a script to execute!

The general steps to fix this error includes:

  1. Clear cache, remove node_modules and reinstall
  2. Check your machine specifications
  3. Check your folder permissions
  4. Check for any ports and processes already in use

1. Clear cache, remove node_modules and reinstall

One option that worked well for NPM issues for me is to nuke the whole project - removing node_modules and reinstall everything.

To do this we can use the steps, open up the terminal and make sure you are in the root directory of the project:

  1. npm install -g npm@latest to update npm because it is sometimes buggy.
  2. rm -rf node_modules to remove the existing modules.
  3. Run npm cache clean --force to make sure we don’t have random modules from the cache.
  4. Execute npm install to re-install the project dependencies.

Why do I need to use npm cache clean?

You may have noticed that we use the cache clean command like so: npm cache clean --force

Sometimes, your dependencies are correct, but you still get the conflicting peer dependency error. This could be due to NPM still looking through the cached modules.

NPM usually stores modules in your local project folder and also a “cached” folder. This will be ~/.npm on linux/ OSX systems, or %AppData%/npm-cache for windows systems.

So the next time you install a similar module it will go off to the cache instead of downloading it! The above command just clears the cache folder.

2. Check your machine RAM/ memory specifications

A common scenario that we can see this error of “err code elifecycle” pop up is on machines that got low memory.

As an example, if you are working on a angular app and require the use of @angular/cli, then a computer with only 1GB of RAM is not going to cut it.

Or if you are using a library that requires high memory - such as image processing.

An example of the error is as below:

  

npm ERR! Linux 3.2.0-4-amd64
npm ERR! argv "/root/.nvm/versions/io.js/v1.6.1/bin/iojs" "/root/.nvm/versions/io.js/v1.6.1/bin/npm" "run" "live"
npm ERR! node v1.6.1
npm ERR! npm  v2.7.1
npm ERR! code ELIFECYCLE
npm ERR! emailer@0.0.0 live: `NODE_ENV=production node app.js`
npm ERR! Exit status 137
npm ERR! 
npm ERR! Failed at the emailer@0.0.0 live script 'NODE_ENV=production node app.js'.
npm ERR! This is most likely a problem with the emailer package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     NODE_ENV=production node app.js
npm ERR! You can get their info via:
npm ERR!     npm owner ls emailer
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /apps/emailer/npm-debug.log

In the above error log, we can see the line: npm ERR! Exit status 137. Exit status of 137 usually means that it failed due to running out of memory!

The fix will have to be bumping up the RAM to something thats more than 1GB!

3. Check your folder permissions

One thing to check is the folder permissions of node_modules.

If you are on Linux then:

  1. Open up the terminal and go to your project’s root folder
  2. Run the following command to update your node_module’s folder permission like so:

chmod -R a+rwx ./node_modules

4. Check for any ports and processes already in use

  

Error: listen EACCES 127.0.0.1:8080

at Object._errnoException (util.js:999:13)
at _exceptionWithHostPort (util.js:1020:20)
at Server.setupListenHandle [as _listen2] (net.js:1362:19)
at listenInCluster (net.js:1420:12)
at GetAddrInfoReqWrap.doListen [as callback] (net.js:1535:7)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:102:10)
npm ERR! code ELIFECYCLE
npm ERR! errno 1

The port is probably being used by another application, try listing and see if it’s your application:

lsof -i:8080

You can kill the process of this port:

lsof -ti:8080 | xargs kill

On Windows we can do the same check as follows:

netstat -ano | findstr :<port number> Replace with the actual port number you want to search for.

This command uses the netstat command to display a list of active network connections and their status, along with the process ID (PID) associated with each connection. The findstr command is then used to filter the results to only show connections that match the specified port number.

The output of this command will show you the PID of the process that is using the specified port number. You can then use the Task Manager or PowerShell to find and terminate the process if needed.

Killing the process

Press the Windows key + R on your keyboard to open the Run dialog box.

Type “cmd” and press Enter to open the Command Prompt.

Type the following command to display a list of running processes and their IDs:

tasklist Find the process you want to kill in the list and note its Process ID (PID).

Type the following command to kill the process with the specified PID:

taskkill /pid <PID> /f Replace with the actual Process ID of the process you want to kill.

The /f option is used to force the process to terminate immediately.

Summary

In this post, I went over the issue of err code elifecycle. This error just means that the script, specified in the package.json file, that NPM is trying to execute came across errors.

The error is related to the NPM lifecycle!

To get around this error, it is common to do the nuke approach. We clear the cache, node_modules and run npm install again.

If this does not work, we have to inspect the error log further. Since this error is rather generic, more information on why the script failed could be listed in the error log.

A checklist of things verify is as follows:

  • check that you have the right permissions for your node_modules directory,
  • verify that your machine got enough memory - could be required for heavy NPM packages such as @angular/cli
  • If you are running a live server, Kill off any process that might be using the same ports!

👋 About the Author

G'day! I am Huy a software engineer based in Australia. I have been creating design-centered software for the last 10 years both professionally and as a passion.

My aim to share what I have learnt with you! (and to help me remember 😅)

Follow along on Twitter , GitHub and YouTube