Britto Saji
Britto Saji

Reputation: 110

node modules not found after packaging electron app with electron-forge for window

Electron app was initialized using electron-forge webpack template and everything works perfectly for macOs. While running the dev version using electron-forge start the app loads perfectly on windows. Once the app is packaged for windows using electron-forge make the build completes successfully. But while running the packaged app Cannot find module X is thrown. The folder ./out/app/resources/app/node_modules is empty. Also the package.json ./out/app/resources/app/package.json looks as follows.

  "name": "my-app",
  "productName": "my-app",
  "version": "1.0.0",
  "description": "My Electron application description",
  "main": ".webpack/main",
  "scripts": {
    "start": "electron-forge start",
    "package": "electron-forge package",
    "make": "electron-forge make",
    "publish": "electron-forge publish",
    "lint": "echo \"No linting configured\""
  },
  "keywords": [],
  "author": {
    "name": "",
    "email": ""
  },
  "license": "MIT",
  "config": {},
  "dependencies": {},
  "devDependencies": {},
  "optionalDependencies": {},
  "peerDependencies": {}
}

None of the dependencies in the source package.json made it to the packaged app.

Upvotes: 10

Views: 2875

Answers (2)

I found the issue in the Webpack plugin.

Look at

https://github.com/electron/forge/blob/2fe0f5d3dbc6047af81188041672fd135a46b99f/packages/plugin/webpack/src/WebpackPlugin.ts#L385

It's just ignoring everything that is not .webpack.

So you have two options:

  1. Define your own ignore function inside Electron's packagerConfig following this schema:

https://www.electronforge.io/config/configuration#electron-packager-config

  1. Define a hook to copy over all that you need.
packageAfterCopy: async (_, appResources) => {
      if(appResources == null)
        throw new Error(`Unknown platform ${options.platform}`);

      const srcNodeModules = path.join(__dirname, 'node_modules');
      const destNodeModules =  path.join(appResources, 'node_modules');
      fs.cpSync( srcNodeModules, destNodeModules, {recursive: true});
    }

Upvotes: 1

kakyo
kakyo

Reputation: 11640

Try this

cd "my-app"

:: install dependency into your node_modules and update your package.json
npm install your-dependency --save-prod
npm run make

Upvotes: -3

Related Questions