How to solve npm ERR! code 1 when running npm install

The npm ERR! code 1 error usually occurs when you run the npm install command.

This cause of this error is that one of the dependencies you define in your package.json file fails to be installed properly on your computer.

The error message log looks as follows:

$ npm install
npm ERR! code 1
npm ERR! path /n-app/node_modules/node-sass
npm ERR! command failed
npm ERR! command sh -c node scripts/build.js
...
npm ERR! Build failed with error code: 1

npm ERR! A complete log of this run can be found in:
npm ERR!    .npm/_logs/2022-06-24T03_37_26_246Z-debug-0.log

In the example above, you can see that there is an npm ERR! path... defined right below the code 1 log:

$ npm install
npm ERR! code 1
npm ERR! path /n-app/node_modules/node-sass

This means that npm fails to install the node-sass module that’s added as a dependency to the n-app project.

To resolve this error, you need to upgrade the dependency module that causes the error.

Let’s learn how to fix this error next.

Fix npm error code 1 by updating your dependencies

Here’s the package.json file of the n-app project:

{
  "name": "n-app",
  "version": "1.0.0",
  "dependencies": {
    "axios": "^0.10.2",
    "node-sass": "^4.14.1",
    "webpack": "^4.0.0"
  }
}

As you can see, there’s a dependency to the node-sass module version 4.14.1 in my project.

When looking into the node-sass documentation, I found that there’s a table showing the Node versions supported by the package:

Since I have Node v16.15.1 on my computer, this means I must use node-sass v6.0+ to resolve the issue.

Update the node-sass dependency to the latest version:

{
  "dependencies": {
    "axios": "^0.10.2",
    "node-sass": "^7.0.1",
    "webpack": "^4.0.0"
  }
}

Then run npm install again. This time, the dependencies are installed without any error:

$ npm install
added 307 packages, and audited 308 packages in 50s

found 0 vulnerabilities

It’s always recommended to update all your dependency packages to the latest version to avoid compatibility issues with Node.

You can use npm-check-updates to help you with updating the dependencies:

# 👇 check your package.json for dependency updates
npx npm-check-updates

# 👇 write newer version in package.json
npx npm-check-updates -u

# 👇 install the latest version
npm install

If you don’t want to update all dependencies at once, you can update them one by one using the npx npm-check-updates output as a reference.

For example, here’s the output for n-app project:

$ npx npm-check-updates
Checking /n-app/package.json
[====================] 3/3 100%

 axios      ^0.21.2  →  ^0.27.2     
 node-sass   ^4.0.1  →   ^7.0.1     
 webpack    ^4.73.0  →  ^5.73.0     

Run npx npm-check-updates -u to upgrade package.json

Using the output above:

  • Update one package version in package.json file
  • Then run npm install
  • Make sure your application runs perfectly and pass all tests

Repeat the steps above until you have all package versions updated.

The other way to fix the issue is to downgrade your Node.js version.

In the example above, there are specific Node.js versions supported by specific node-sass versions. You need to make sure that you are using the Node version supported by your module.

This fix is not ideal because you may have other module with compatibility issues once you downgrade Node.

The best way to solve the issue is by updating Node.js and all your dependencies to the latest stable version as explained above.

And that’s how you fix the npm error code 1 issue in your project.

Take your skills to the next level ⚡️

I'm sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.