node-sass: Command failed when running npm install [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 4, 2024
3 min

banner

# node-sass: Command failed when running npm install [Solved]

The error "node-sass: Command failed" occurs for multiple reasons:

  1. Using the deprecated node-sass module.
  2. Using a version of node-sass that is not compatible with your Node.js version.
  3. Using an outdated version of Create React app.
  4. Having a glitched node_modules directory.
shell
error /node_modules/node-sass: Command failed. Command: node scripts/build.js 1837 error command failed 1837 error gyp ERR! not ok 1837 error gyp info it worked if it ends with ok Exit code: 1

The two most common ways to solve the error are to:

  1. Install the sass package instead of the deprecated node-sass module.
  2. Install a version of node-sass that is compatible with your version of Node.js.

The node-sass module has been deprecated, so the best way to solve the error is to use the sass module instead.

Open your terminal in your project's root directory and run the following commands.

shell
# ๐Ÿ‘‡๏ธ with NPM npm uninstall node-sass npm install sass --save-dev # ๐Ÿ‘‡๏ธ or 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.

# Delete your node_modules and reinstall your dependencies

If the error persists, try to delete your node_modules and package-lock.json (not package.json) files, rerun the npm install command and restart your dev server.

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

Try to restart your development server after reinstalling your modules.

If you want to use the deprecated node-sass module, make sure you have a compatible node-sass version installed for your version of Node.js.

# Alternatively, 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 # ๐Ÿ‘‡๏ธ 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.

If the error persists, try to delete your node_modules and package-lock.json (not package.json) files, rerun the npm install command and restart your dev server.

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

Try to restart your development server after reinstalling your modules.

If the error persists and you use "Create React app", try to update your version of react-scripts.

# Try to update your version of react-scripts

Open your terminal and run the following command to update your version of react-scripts.

shell
# ๐Ÿ‘‡๏ธ if you use npm npm install react-scripts@latest # ๐Ÿ‘‡๏ธ if you use yarn yarn add react-scripts@latest

npm install react scripts latest

If the error persists, try to delete your node_modules and package-lock.json (not package.json) files, rerun the npm install and restart your dev server.

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 npm install react-scripts@latest

Try to restart your development server after updating your react-scripts version.

# Conclusion

To solve the error "node-sass: Command failed" error:

  1. Use the sass package instead of the deprecated node-sass module.
  2. Alternatively, install a version of node-sass that is compatible with your version of Node.js.
  3. Delete your node_modules directory and your package-lock.json files and rerun npm install.
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