Bundle error using webpack for Electron application `Cannot reso

ghz 6months ago ⋅ 57 views

Bundle error using webpack for Electron application Cannot resolve module 'electron'

I am trying to create an Electron application with React. I use Webpack to compile the React JSX syntax, but when I try to compile with webpack command, I got this error:

ERROR in ./app.jsx Module not found: Error: Cannot resolve module 'electron' in /Users/masterT/Downloads/gist

@ ./app.jsx 6:18-37

Here is the application code.

I am doing something wrong?

Answers

The error message you are seeing indicates that Webpack cannot resolve the electron module. This is because electron is a native module that isn't intended to be bundled by Webpack in the same way as other dependencies.

To resolve this issue, you need to make a few adjustments to your Webpack configuration and project setup:

  1. Separate Main and Renderer Processes: Ensure that your Electron application's main process and renderer process are properly separated. Typically, you will have two Webpack configurations: one for the main process and one for the renderer process (React application).

  2. Use externals for electron: In your Webpack configuration for the renderer process, mark electron as an external dependency. This tells Webpack not to bundle electron with your React code.

Here are the steps to achieve this:

  1. Directory Structure: Ensure your project structure separates the main process and renderer process:

    /project-root
      /src
        /main
          main.js
        /renderer
          app.jsx
          index.html
      package.json
      webpack.main.config.js
      webpack.renderer.config.js
    
  2. Webpack Configuration for Main Process (webpack.main.config.js): This configuration is for your Electron main process.

    const path = require('path');
    
    module.exports = {
      entry: './src/main/main.js',
      target: 'electron-main',
      output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'main.js'
      },
      node: {
        __dirname: false,
        __filename: false
      }
    };
    
  3. Webpack Configuration for Renderer Process (webpack.renderer.config.js): This configuration is for your React application (renderer process).

    const path = require('path');
    
    module.exports = {
      entry: './src/renderer/app.jsx',
      target: 'electron-renderer',
      module: {
        rules: [
          {
            test: /\.jsx?$/,
            exclude: /node_modules/,
            use: 'babel-loader'
          }
        ]
      },
      resolve: {
        extensions: ['.js', '.jsx']
      },
      output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'renderer.js'
      },
      externals: {
        electron: 'require("electron")'
      }
    };
    
  4. Install Necessary Dependencies: Ensure you have the required dependencies installed.

    npm install --save-dev webpack webpack-cli babel-loader @babel/core @babel/preset-env @babel/preset-react
    npm install react react-dom electron
    
  5. Babel Configuration (.babelrc): Add a Babel configuration file to transpile your JSX.

    {
      "presets": ["@babel/preset-env", "@babel/preset-react"]
    }
    
  6. Package Scripts (package.json): Update your package.json to include scripts for building both main and renderer processes.

    {
      "name": "your-electron-app",
      "version": "1.0.0",
      "main": "dist/main.js",
      "scripts": {
        "build-main": "webpack --config webpack.main.config.js",
        "build-renderer": "webpack --config webpack.renderer.config.js",
        "build": "npm run build-main && npm run build-renderer",
        "start": "electron ."
      },
      "devDependencies": {
        "webpack": "^5.x.x",
        "webpack-cli": "^4.x.x",
        "babel-loader": "^8.x.x",
        "@babel/core": "^7.x.x",
        "@babel/preset-env": "^7.x.x",
        "@babel/preset-react": "^7.x.x"
      },
      "dependencies": {
        "react": "^17.x.x",
        "react-dom": "^17.x.x",
        "electron": "^13.x.x"
      }
    }
    
  7. Running the Application:

    • Build the application using npm run build.
    • Start the Electron application using npm start.

By following these steps, you should be able to compile your Electron application with React using Webpack without encountering the module resolution error for electron.