Module not found: Error: Can't resolve 'fs' in

Solution 1:

The error is because of angular-cli does not support modules in node like "fs" and "path". (Issue)

Add the following to the root of the "package.json" file.

"browser": {
  "fs": false,
  "path": false,
  "os": false
}

I hope this helps someone.

Thanks.

Solution 2:

For me the solution was to add this to the webpack config:

config.node = {
  fs: 'empty',
}

Another solution if you use NuxtJS:

// Build Configuration: https://go.nuxtjs.dev/config-build
build: {
    extend(config, {}) {
        config.node = {
            fs: 'empty'
        }
    }
},

If you use NextJS (not tested, please confirm that it works in comments):

webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
  config.node = {
    fs: 'empty'
  }
  return config
},

In other cases, please refer to Anjana Silva post. An edit of your package.json file can do the job!

Solution 3:

For Webpack > 5
update webpack.config.js

module.exports = {
    ...
    resolve: {
        fallback: {
            "fs": false
        },
    }
}

Can't resolve 'fs' when bundle with webpack #447

Solution 4:

tl;dr

For people transpiling for Node.js: add target: node to the webpack.config.js file.

Explanation

I know this is an Angular question but those who are transpiling for Node.js have to keep in mind that by default, Webpack transpiles for browser targets, where you don't have modules provided only by Node.js. If your environment where you will be running the transpiled code is indeed Node.js then just mark it in the config file with target: node.

Solution 5:

Just add {node:'empty'} to your webpack.config file.