Module not found: Can’t resolve ‘fs’

If you use Webpack version 5 or later you may encounter the error message

Module not found: Can’t resolve ‘fs’

The cause is, that Webpack 5 has a breaking change, and the “fs” module is most likely not included anymore in the client-side code.

To eliminate the issue, add the “browser” element to the package.js file under dependencies:

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

If the error persists, add the “fs”, “os” and “path” elements to the fallback element in the webpack.config.js file:

module.exports = function (webpackEnv) {
  ...
  return {
   ...
    resolve: {
      ...
      fallback: {
        "fs": false,
        "os": false,
        "path": false,
      }
    }
  }
}

Join the Conversation

2 Comments

  1. If I do this, I get this error
    “`fs.readFileSync is not a function“`
    or
    “`fs.readFile is not a function“`
    Any idea why and how to fix it?

Leave a comment

Your email address will not be published. Required fields are marked *