I am trying to run my nextjs App But I am facing the following error in my Developer Console Log: Module not found: Can’t resolve ‘fs’. In this Exerror article, We are going to learn about How to reproduce this error and we will discuss All Possible Solutions Let’s Get Started with This Article.
How Module not found: Can’t resolve ‘fs’ Error Occurs?
I am trying to run my nextjs App But I am facing the following error in my Developer Console Log.
Module not found: Can't resolve 'fs'
So here I am writing all the possible solutions that I have tried to resolve this error.
How To Solve Module not found: Can’t resolve ‘fs’ Error?
- How To Solve Module not found: Can’t resolve ‘fs’ Error?
To Solve Module not found: Can’t resolve ‘fs’ Error You need to add this line in your next.config.js file. If You don’t have next.config.js then create it in the root of your project. If You are using webpack 5 then add this line of code in next.config.js And Now, re-run your next app with npm run dev, and your error may be resolved now.
- Module not found: Can’t resolve ‘fs’
To Solve Module not found: Can’t resolve ‘fs’ Error You need to add this line in your next.config.js file. If You don’t have next.config.js then create it in the root of your project. If You are using webpack 5 then add this line of code in next.config.js And Now, re-run your next app with npm run dev, and your error may be resolved now.
Solution 1: Add this line in next.config.js
You need to add this line in your next.config.js file. If You don’t have next.config.js then create it in the root of your project.
//For Webpack 4
module.exports = {
webpack: (config, { isServer }) => {
if (!isServer) {
config.node = {
fs: 'empty'
}
}
return config
}
}
If You are using webpack 5 then add this line of code in next.config.js
module.exports = {
webpack5: true,
webpack: (config) => {
config.resolve.fallback = { fs: false };
return config;
},
};
And Now, re-run your next app with npm run dev, and your error may be resolved now.
Conclusion
It’s all About this error. I hope We Have solved Your error. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you?
Also, Read
- Error: Cannot find module ‘express’
- TypeError: missing 2 required positional arguments
- ReferenceError: __dirname is not defined in ES module scope
- WebDriverException: unknown error: unexpected command response
- Error: Cannot find module ‘webpack/lib/rules/DescriptionDataMatcherRulePlugin’