Table of Contents
React is a JavaScript library that is used to build user interfaces and it can be used in any JavaScript project that you are working on. When facing the error “Cannot find module ‘sass’ in reactjs”, you might be wondering what should I do. Read all the steps given in this article to help you solve the problem.
Problem “Cannot find module ‘sass’ in reactjs”
The system returned the following error while you were running your react app:
Cannot find module 'sass'
Require stack:
Cause of the error
Understanding the original reason will help you remove the error straight away. After looking for solution for this error, we ended up with 2 main reasons. Firstly, your IDE is lacking of sass module. Secondly, node-sass library is not supported anymore, or you can say, it is deprecated by now. Although this kind of error means the lib will still be maintained and fixed in some future releases, I will no longer have new features or the compatibility with css/scss features.
How to fix it?
To resolve the problem for each kind of cause, We recommend you to try out following methods.
Method 1: Reinstall
Clear cache and install new sass
Run the following statement to clear cache:
npm cache clear --force
Run the following statement to install sass.
If you want to install globally, use this:
npm i -g sass
If you want to install locally, use this:
npm i sass --save-dev
Now let’s check to see if the error disappear. If it hasn’t, the “Cannot find module ‘sass’ in reactjs ” might belong to the second case “node-sass library is not supported anymore” you may need to consider the following method.
Method 2: Change your plugin
Change your plugin from node-sass to Dart Sass then clear your cache forcefully and install sass globally again. The steps to install sass were mentioned in solution 1, so let’s try it.
Method 3: Restart the localhost
Enter the command below in your program:
npm install --save-dev node-sass
Then restart the localhost:
npm start
When a prompt box appears, select enter. Next, check the dependency settings present in package.json and search for “devDependencies”: {“node-sass”: “^yourversion”}
Method 4: Using docklefile
You just need to include this line to your “dockerfile” in order to create your docker image, it will automatically install sass:
RUN npm install -g sass
and within and in your “package.json” include this information in the “dependencies” that instructs the program to implement the code. In this case , which is to say that we have the exact variation that is “sass”:
"sass": "version number(example:^5.0.0)"
and then add this to the “scripts” section that informs the program of how to function in this particular case, I believe it states to programmers that “scss” files, you need to open that file, and then recognize it and open it as it is a “css”:
"scss": "sass --whatch scss -o css"
Conclusion
That was it, four solutions to resolve the error making you crazy “Cannot find module ‘sass’ in reactjs”, I hope this guide works with you guys. If you have any questions or difficulties during the operation, please contact us immediately for quick support. Thanks for reading!
I have another way
Recommendation for new programmer as I did
Follow this way if you want to use scss/sass in your react app:
first, delete node-sass:
if you use npm:
Then install sass instead of node-sass:
or
In short, your scss/sass files will be correctly followed !