Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The error "Module not found: Error: Can't resolve 'fs' in /node_modules/electron" occurs because the 'fs' module is a Node.js core module that is not available in the browser environment. Electron is built on top of Node.js, so it can access the 'fs' module. However, since the module is not available in the browser environment, it cannot be resolved.

To resolve this error, you can use a Webpack module called 'electron-webpack-static' or 'electron-webpack-node-externals' to exclude Node.js core modules from the Webpack bundle. Then, in your code, you can use conditional imports to only use the 'fs' module when running in the Electron environment.

For example, you can write:

if (typeof window === 'undefined') {
  const fs = require('fs');
  // use fs here
}

This code checks if the 'window' object is undefined, which is true in Node.js or Electron environment. If it is, then it loads and uses the 'fs' module.

By using conditional imports like this and excluding Node.js core modules from the bundle, you can avoid the "Module not found: Error: Can't resolve 'fs'" error and make your Electron/React/Typescript module work properly.