forge: electron-forge does not work with npm workspaces (npm v7)

Preflight Checklist

  • I have read the contribution documentation for this project.
  • I agree to follow the code of conduct that this project follows, as appropriate.
  • I have searched the issue tracker for a bug that matches the one I want to file, without success.

Issue Details

  • Electron Forge Version: 6.0.0-beta.57
  • Electron Version: 13.1.0
  • Operating System: macOS 11.4
  • Last Known Working Electron Forge version:: N/A

Expected Behavior

Running electron-forge start from project root (npm run start --workspace="packages/electron-app") or from electron workspace root (cd packages/electron-app && npm run start) should be able to locate the electron executable in the project root node_modules.

Actual Behavior

Running electron-forge start throws a PackageNotFoundError:

An unhandled rejection has occurred inside Forge:
PackageNotFoundError: Cannot find the package "electron". Perhaps you need to run "yarn install" in "<path-to-project-root>/packages/electron-app"?

To Reproduce

Minimal repro example: https://github.com/brightbrained/electron-forge-workspaces

Make sure to have npm v7 or above for workspaces support (npm install --global npm@latest)

git clone https://github.com/brightbrained/electron-forge-workspaces.git
cd electron-forge-workspaces
npm install
npm run start --workspace="packages/electron"

Additional Information

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 5
  • Comments: 24

Commits related to this issue

Most upvoted comments

Here’s a working app using NPM workspaces: https://github.com/jeanbmar/black-moon-rewind/tree/r0.1-beta/packages/launcher In order to get Forge working til the end, I had to write my own dependency resolver (np-bundle) and call it in Forge hooks because electron-packager won’t resolve monorepo deps.

I also had to set packagerConfig: { prune: false } in my forge config file, but then your bundler worked.

Thank you very much!

You should delete lock files in your packages according to source code:

the folder exist a lock file will be treated as the electron module root.

image

Based on @jeanbmar’s resolver I now built my own one, which you can find in this gist. It is quite a bit shorter (and hopefully easier to understand), includes types and correctly skips all non-prod dependencies.

Here’s a working app using NPM workspaces: https://github.com/jeanbmar/black-moon-rewind/tree/r0.1-beta/packages/launcher In order to get Forge working til the end, I had to write my own dependency resolver (np-bundle) and call it in Forge hooks because electron-packager won’t resolve monorepo deps.

I was having this problem but fixed it by deleting a package-lock.json file in the electron subdirectory, allowing it to climb back to the root node_modules and find the electron package.

Here’s a working app using NPM workspaces: https://github.com/jeanbmar/black-moon-rewind/tree/r0.1-beta/packages/launcher In order to get Forge working til the end, I had to write my own dependency resolver (np-bundle) and call it in Forge hooks because electron-packager won’t resolve monorepo deps.

I come back to my previous comment, webpack-typescript setup was not much better. This solution worked for a while until I got the following error

An unhandled rejection has occurred inside Forge:
RangeError: Maximum call stack size exceeded
at ...path-to-workspace/apps/electron-app/bundler.js:52:14
    at Array.flatMap (<anonymous>)
    at collectProdDeps (...path-to-workspace/apps/electron-app/bundler.js:52:6)
    at ...path-to-workspace/apps/electron-app/bundler.js:52:40
    at Array.flatMap (<anonymous>)
    at collectProdDeps (...path-to-workspace/apps/electron-app/bundler.js:52:6)
    at ...path-to-workspace/apps/electron-app/bundler.js:52:40
    at Array.flatMap (<anonymous>)
    at collectProdDeps (...path-to-workspace/apps/electron-app/bundler.js:52:6)
    at ...path-to-workspace/apps/electron-app/bundler.js:52:40
    at Array.flatMap (<anonymous>)
    at collectProdDeps (...path-to-workspace/apps/electron-app/bundler.js:52:6)
    at ...path-to-workspace/apps/electron-app/bundler.js:52:40
    at Array.flatMap (<anonymous>)
    at collectProdDeps (...path-to-workspace/apps/electron-app/bundler.js:52:6)
    at ...path-to-workspace/apps/electron-app/bundler.js:52:40

I solved this by updating the collectProdDeps to

/** @type {(node: Node) => Node[]} */
const collectProdDeps = (node) => {
  const stack = [node];
  const result = new Set();  // Using a set to avoid duplicates and track visited nodes

  while (stack.length > 0) {
    const currentNode = stack.pop();
    const depEdges = [...currentNode.edgesOut.values()].filter((depEdge) => depEdge.type === "prod");

    for (const depEdge of depEdges) {
      const depNode = resolveLink(depEdge.to);

      if (!result.has(depNode)) {
        result.add(depNode);
        stack.push(depNode);
      }
    }
  }

  return Array.from(result);  // Convert the set to an array
};

Which seems to bundle the app properly.

Can’t wait for electron-forge to “just” support this.

What do you do about electron packager and copying node modules over? Or you aren’t using workspaces?