Module not found: Error: Can't resolve 'fs' when trying to use a NodeJS library
I initiated a basic ReactJS app using npx create-react-app
, then I ejected using npm run eject
. Now when I am trying to import the Casual library by import casual from 'casual';
, I get the following error:
Compiled with problems:
ERROR in ./node_modules/casual/src/casual.js 3:13-37
Module not found: Error: Can't resolve 'fs'
in '/home/me/project/node_modules/casual/src'
And the code around line number 3 in casual.js
looks like this:
var helpers = require('./helpers');
var exists = require('fs').existsSync;
var safe_require = function(filename) {
if (exists(filename + '.js')) {
return require(filename);
}
return {};
};
...
I found answers to similar questions. Those were mainly Node or Angular related. I also tried answers suggesting some changes in webpack config, but no luck.
The reason is Casual
doesn't work on the front end. It runs on Node.js only.
You need to install maybe a new package to make things work.
Fs
is unavailable on the browser so it won't work. Instead, you should use casual-browserify, it will work on browsers.