npm ERR! code 1 command failed [npm install error Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 4, 2024
4 min

banner

# npm ERR! code 1 command failed [npm install error Solved]

The error "npm ERR! code 1 command failed" occurs when you aren't able to install a specific NPM package or a specific version of an NPM package with your version of Node.js.

To solve the error, install a version of the package that is compatible with your Node.js version and update your dependencies.

shell
npm ERR! code 1 npm ERR! path /home/bobbyhadz/Desktop/react-projects/ecommerce/node_modules/node-sass npm ERR! command failed npm ERR! gyp verb cli [ npm ERR! gyp verb cli '/usr/local/bin/node', npm ERR! gyp verb cli '/home/bobbyhadz/Desktop/react-projects/ecommerce/node_modules/node-gyp/bin/node-gyp.js', npm ERR! gyp verb cli 'rebuild', npm ERR! gyp verb cli '--verbose', npm ERR! gyp verb cli '--libsass_ext=', npm ERR! gyp verb cli '--libsass_cflags=', npm ERR! gyp verb cli '--libsass_ldflags=', npm ERR! gyp verb cli '--libsass_library=' npm ERR! gyp verb cli ] npm ERR! gyp info it worked if it ends with ok npm ERR! Build failed with error code: 1

You can check which package caused the error on the second line of your error message.

shell
# ๐Ÿ‘‡๏ธ node-sass npm ERR! path /home/bobbyhadz/node_modules/node-sass

The first thing you should try is to install the latest version of the package.

shell
npm install node-sass@latest --legacy-peer-deps

Make sure to replace node-sass with the name of the package that caused the error in your case.

If you didn't get the error when trying to install node-sass scroll down to the next subheading.

If you got the error when installing the node-sass package, install the sass package instead because node-sass has been deprecated.

Open your terminal in your project's root directory (where your package.json file is) and run the following commands.

shell
# ๐Ÿ‘‡๏ธ with NPM npm uninstall node-sass npm install sass --save-dev # ๐Ÿ‘‡๏ธ with YARN yarn remove node-sass yarn add sass --dev

The two commands uninstall the deprecated node-sass module and install the sass module.

Try to restart your development server after running the commands.

Alternatively, you can install a compatible version of node-sass.

If you'd rather use the deprecated node-sass module, you have to install a version of node-sass that is compatible with your Node.js version.

The table in this section of the node-sass npm page shows which node-sass version you should install for your version of Node.js.

You can use the node --version command to get your version of Node.js.

shell
node --version

get node version

Here is part of the table.

NodeJS versionSupported node-sass version
Node 198.0+
Node 188.0+
Node 177.0+, <8.0
Node 166.0+
Node 155.0+, <7.0
Node 144.14+
Node 134.13+, <5.0

For example, if your Node.js version is 17, you would install version 7 of node-sass.

shell
# ๐Ÿ‘‡๏ธ if your Node.js version is 17 npm install node-sass@7 --save-dev --legacy-peer-deps # ๐Ÿ‘‡๏ธ for YARN with Node.js version 17 yarn add node-sass@7 --dev

Your node-sass version has to be compatible with your Node.js version, otherwise, the error occurs.

You can check your node-sass version in the devDependencies section of your package.json file.

# Update the version of your NPM packages

If you have a recent version of Node.js installed and your package.json file contains older versions of packages that are not compatible with your Node.js version, the error occurs.

You can use the npm-check-updates package to update your package.json dependencies to the latest version.

Open your terminal in your project's root directory (where your package.json file is) and run the following command.

You can add your package.json file to version control (e.g. git) because the following 2 commands will update the versions of your packages in your package.json file.
shell
npx npm-check-updates -u npm install

If the error persists, try to delete node_modules and package-lock.json and reinstall.

# Try to reinstall your node_modules

If the error is not resolved, try to delete your node_modules and package-lock.json (not package.json) files, rerun npm install and restart your IDE.

shell
# ๐Ÿ‘‡๏ธ (Windows) delete node_modules and package-lock.json rd /s /q "node_modules" del package-lock.json del -f yarn.lock # ๐Ÿ‘‡๏ธ (macOS/Linux) delete node_modules and package-lock.json rm -rf node_modules rm -f package-lock.json rm -f yarn.lock # ๐Ÿ‘‡๏ธ clean npm cache npm cache clean --force npm install
Make sure to restart your IDE and dev server if the error persists.

# Try to update your npm version

Try to update your NPM version by running the following command.

shell
npm install -g npm@latest # ๐Ÿ‘‡๏ธ If you get a permissions error on macOS / Linux sudo npm install -g npm@latest

update npm version

If you get a permissions error on Windows, open CMD as an administrator and rerun the command.

To open CMD as an administrator:

  1. Click on the Search bar and type CMD.

  2. Right-click on the Command Prompt application and click "Run as administrator".

run cmd as administrator

  1. Rerun the command.
shell
npm install -g npm@latest npm install -g npm@latest --force

Try to run the npm install command after updating your npm version.

If the error persists, try to install the long-term supported version of Node.js.

# Update your Node.js version to the long-term supported one

You might be using a Node.js version that is not supported by the packages you're trying to install.

This can happen when you use a Node.js version that is too recent, so support hasn't been added or a Node.js version that is out of date and is no longer supported by the packages you're trying to install.

You can check your Node.js version with the node --version command.

You can use the node --version command to get your Node.js version.

shell
node --version

get node version

If you use nvm, you can update your version of Node.js and npm with the following 2 commands.

shell
nvm install --lts nvm use --lts

install lts node js version

You can also use the n package to manage your Node.js version.

shell
npm cache clean -f npm install -g n n stable

If you get a permissions error when issuing the commands, open CMD as an administrator (Windows) or prefix the commands with sudo (macOS and Linux).

shell
sudo npm cache clean -f sudo npm install -g n sudo n stable

Alternatively, you can download the long-term supported version of Node.js from the official nodejs.org website.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev