Can’t resolve ‘fs’ using webpack and react

You can’t use any modules that come with the Node Core API in the browser, they just won’t work there. Node Core Modules rely on C/C++ binaries which won’t be present in the browser. So you’ll need to find an equivalent approach to replacing any fs use in your react app.

One approach would be to serve the file you want to work with over HTTP via a Node JS backend to your React app. You could read the file using fs in your Node server, and place that logic within a route, call that route from your React App via HTTP and stream it back. Then you will have the file data within your React App and can work with it as you need.

This GitHub repo has a listing of Node Core Libraries that Webpack has ported over for browser usage. Unfortunately fs isn’t one of them.

Leave a Comment