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

2

9

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.

Alphonsa answered 23/2, 2020 at 14:39 Comment(0)
P
0

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});
    }
Pneumatophore answered 28/1 at 12:36 Comment(0)
L
-2

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
Latchet answered 30/8, 2020 at 5:33 Comment(1)
will this command copy the node_modules folder into the resources folder? Second will the paths break if I have development paths? #76398070Vulnerary

© 2022 - 2024 — McMap. All rights reserved.