How to fix npm start error: rebuild node-sass issue

Photo from Unsplash

The node-sass package is a library that provides binding for Node.js to LibSass, which is the Sass processsor written in C++.

When running a project that has a dependency on node-sass, you may see an error that says you need to run npm rebuild node-sass as shown below:

$ npm start

Error: Missing binding 

Node Sass could not find a binding for your current environment: 
Linux 64-bit with Node.js 12.x

Found bindings for the following environments:
  - Linux 64-bit with Node.js 10.x

This usually happens because your environment has changed 
since running `npm install`.
Run `npm rebuild node-sass` to download the binding 
for your current environment.

To make node-sass available in your source code, npm needs to build the package and create a binding for your environment.

When you change the version of Node.js used on your project, then the binding needs to be rebuild so that node-sass can be imported and executed from your code.

As you can see in the example above, there is a binding for Node.js 10.x but none is found for Node.js 12.x

To fix the issue, you need to run npm rebuild node-sass as suggested by the error response:

npm rebuild node-sass

Once the rebuild is finished, run the project again.

If that doesn’t work, then you need to make sure that your node-sass and Node.js versions are compatible:

Update your node-sass package to the supported version number, then run the following commands step by step:

# 👇 remove node_modules and package-lock.json
rm -rf node_modules && rm package-lock.json

# 👇 run npm install to get node-sass
npm install

# 👇 optional: rebuild node-sass binding
npm rebuild node-sass

Once done, you should be able to run the project without any errors.

As you can see, you need to constantly rebuild node-sass each time you change the environment to create the binding.

Furthermore, the node-sass package has been deprecated in October 2020, so it won’t receive new features and will disappear in the future.

To prevent future issues, you need to replace node-sass with the sass package:

sass npm package

The new sass package is a distribution of Sass written in Dart.

This sass package is pure JavaScript, which means you don’t need to build the package to make it work with Node.js.

Currently, sass supports partial compatibility with node-sass JavaScript API so you can call the render() and renderSync() functions.

The node-sass API is expected to be removed in sass version 2.0.0 though, so make sure you migrate your code properly should you decide to use it.

You can visit this blog post for more information:

Sass blog: LibSass is deprecated

And now you’ve learned why you need to rebuild node-sass frequently.

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.