How to Fix NPM Error Code 127
Checklist to fix the error NPM Error Code 127!Jan 28, 2023 | Read time 10 minutes
🔔 Table of contents
Have you ever run into the issue of getting the error “NPM Error Code 127”? I found out I had this error when trying to rebuild my React application a few weeks ago.
Funnily enough, this error code comes from your Linux distro /Mac OSX rather than Node/NPM itself.
“Error Code 127” just means that process that is running had to be exited because it cannot find the command that was specified! The command is not listed in the PATH system environment variable. In my case, I was missing NPM and the react-scripts CLI.
This error is common when you are first building your code on a fresh system, or deploying code with Docker.
The error comes in many forms and typically looks like the following:
Sending build context to Docker daemon 453.6 kB Sending build context to Docker daemon Step 0 : FROM ubuntu:14.04 ---> 37a9c4a8276c Step 1 : WORKDIR /app ---> Using cache ---> a83d4ef27948 Step 2 : COPY . /app ---> 1029f5d7d8a3 Removing intermediate container eb9e7ea7f7e6 Step 3 : RUN npm install ---> Running in 5d4f2c05d2d8 /bin/sh: 1: npm: not found INFO The command [/bin/sh -c npm install] returned a non-zero code: 127
For React projects we can see something like this:
sh: react-scripts: command not found npm ERR! file sh npm ERR! code ELIFECYCLE npm ERR! errno ENOENT npm ERR! syscall spawn npm ERR! email@example.com start: `react-scripts start` npm ERR! spawn ENOENT npm ERR! npm ERR! Failed at the firstname.lastname@example.org start script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
If you are using yarn you can see similar errors:
yarn start yarn run v1.3.2 $ react-scripts start /bin/sh: react-scripts: command not found error Command failed with exit code 127.
What are exit codes anyway?
In this post, I we will step through a checklist on how to fix this error.
To fix the “NPM Error Code 127” we can do the following:
- Check your system PATH environment variable to see if you have the correct command
- Clear NPM node_modules and package-lock.json and reinstall
- Check to see if you have the right versions of Node/NPM
1. Check your system PATH environment variable to see if you have the correct command
The error code 127 is complaining that it could not find the command you are trying to execute in your build process. So the first thing we have to look into is making sure that the command works.
We can do the following:
- Open up the terminal
- Do a test to check if the command will work - type in the command you are trying to execute. In my case, I had to check if react-scripts worked just purely from the command line.
For an example, if you run the following command to check NPM and Node versions:
npm --version node --version
If it is spitting out an error of “command not found” then we know that it is not in the $PATH. Go to step 6 to add your NPM and Node paths to the $PATH environment variable!
- Run the command:
echo $PATH. This will print out something like the following. Check that your path to Node/NPM exists here.
$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
- We can also check the location of your NPM/Node commands with with the
$ which node /usr/bin/node
Make sure that the $PATH to the command is correct and also you are not misspelling the command - eg
If the path to your command does not exists in the $PATH system environment variable, we can add it with the
As an example, if I wanted to add the path to
/bin/nodescripts, I can do the following:
$ export PATH="/bin/nodescripts:$PATH"
This way, any scripts that run under the folder
/bin/nodescripts can be accessed on your system (do not have to navigate to that folder!)
You can then verify this, with the
$ echo $PATH /bin/nodescripts [...]
React Tip: Check path and folder with spaces!
2. Clear NPM node_modules and package-lock.json and reinstall
A typical way to fix this issue, to clear and reinstall everything. Probably do this as a last resort, but make sure you have backups of your work first (eg they are checked in or kept in a backup somewhere).
For my example, I had this issue with react-scripts missing, so did the following.
Your situation might be different - just replace
react-scripts with the command that the error is spitting out:
- Firstly, open up the terminal and go to the root of your project directory
Tip: Try clear NPM cache
We can try running
npm cache clear --forceto clear the NPM cache. If this does not work - proceed to step 2
- We need to delete the /node_modules with the following command (you might need to use
sudobefore each command):
rm -rf node_modules
- Delete package-lock.json file using the rm command:
rm -rf package-lock.json
- Then install the react-scripts package:
npm install react-scripts
- Install the dependencies using the following command:
This would hopefully build correctly, and so you can start the local server:
npm run start
Tip: Commands for Yarn
If you are using Yarn, then the number of commands are a bit less:
- clear the node_modules folder
yarn run start
3. Check to see if you have the right versions of Node/NPM
Sometimes by just using the older versions of Node or NPM, this error can come up.
- Firstly, run
npm cache clear --force
- Check your node and NPM version and verify if it is the latest versions
npm --version node --version
If the versions are not the latest LTS versions, then continue to step 3 to upgrade:
- Upgrade to the latest version of Node
If you’re using nvm, you can use the command nvm install [version] to install a specific version of Node.js, and nvm use [version] to switch to that version.
If you’re using npm, you can use the command npm install -g n and then n [version] to install and switch to a specific version of Node.js.
- Upgrade to the latest version of NPM
To upgrade to the latest version of npm, you can use the command npm install -g npm@latest. This will install the latest version of npm globally on your system.
Note that you may need to use sudo or run the command prompt as an administrator, depending on your system configuration.
Tip: Fixes for Docker
If you are seeing this error in Docker, then it just means that you are using a image of docker that does not have NPM or Node installed!
Review your docker image and use a image that contains node pre-installed - https://hub.docker.com/_/node
For example, don’t just use a basic “unbuntu” image - since this might not come with node or NPM
What about ERR exit code 127 in deployment pipelines?
This error comes up frequently when we are running our deployment or build pipelines. For example, lets say you are using Vercel or AzureDevOps as your build and deploy pipelines.
We can encounter this error 127 with Parcel
[21:07:21.022] Cloning completed: 371.994ms [21:07:21.103] Installing build runtime... [21:07:22.998] Build runtime installed: 1.894s [21:07:23.868] No Build Cache available [21:07:24.021] Installing dependencies... [21:07:25.307] [21:07:25.308] added 17 packages in 1s [21:07:25.308] [21:07:25.308] 2 packages are looking for funding [21:07:25.308] run `npm fund` for details [21:07:25.510] Detected `package-lock.json` generated by npm 7... [21:07:25.510] Running "npm run build" [21:07:25.783] [21:07:25.784] > email@example.com build [21:07:25.784] > parcel build src/index.html [21:07:25.784] [21:07:25.789] sh: parcel: command not found [21:07:25.798] Error: Command "npm run build" exited with 127
To fix this we just need to add a local devDependency for parcel for the build to run sucessfully
npm install --save-dev parcel
The ERR exit code 127 just means that the process that is currently executing could not find the specific command and therefore exited.
To fix this error, we can check our environment $PATH (add the locations when its missing), update to the latest versions of NPM and Node, and clear node_modules and package-lock.json and reinstall everytthing with npm install!