How to Fix Error: Cannot find module 'node-sass' in JavaScript

The error “Cannot find module ’node-sass’” occurs when Node can’t find the node-sass package required in your project.

Suppose you import the node-sass module in your code as follows:

var sass = require('node-sass');

sass.render({
  file: scss_filename,
}, function(err, result) {
  console.log(result)
});

When running the application, you might encounter an error like this:

  throw err;
  ^

Error: Cannot find module 'node-sass'
Require stack:
- /Users/nsebhastian/Desktop/DEV/nodejs/n-app/index.js

This error indicates that Node tried to import the node-sass module, but failed.

To resolve this error, try installing the module using npm or Yarn with the following commands:

npm install node-sass

# For Yarn
yarn add node-sass

Once the installation process is finished, run your application again. The error should now be resolved.

If you still see this error, try to perform a clean install before running npm install again.

Make sure that you have the node-sass package in your package.json file under the dependencies config:

{
  "dependencies": {
    "node-sass": "^7.0.1", // make sure this exists
    // rest of dependencies
  }
}

Next, use the following commands to delete the node_modules folder, clear your cache, and remove the lock files:

# for Windows
rd /s /q "node_modules"
del package-lock.json
del -f yarn.lock

# For Mac/Linux
rm -rf node_modules
rm package-lock.json
rm yarn.lock

# Clean npm cache
npm cache clean --force

# Install the package
npm install

Now that the packages are freshly installed, run your Node application again.

And that’s how you fix the error “Cannot find module ’node-sass’” in your Node.js project.

I have more articles explaining some key Node.js issues, such as:

These articles might help you in developing a Node.js application. Happy coding!

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.