electron-builder icon indicating copy to clipboard operation
electron-builder copied to clipboard

Does not work with NPM workspaces

Open ghost opened this issue 1 year ago • 20 comments

  • Electron-Builder Version: 23.3.3
  • Node Version: v18.3.0
  • Electron Version: 20.1.0

  • Electron Type (current, beta, nightly): current

  • Target: win32 x64

electron builder does not work in npm workspace it deletes app-builder-bin during "installing production dependencies" and then throws error:

• packaging       platform=win32 arch=x64 electron=20.1.0 appOutDir=dist\win-unpacked
The system cannot find the path specified.
  ⨯ spawn C:\Users\user\dev\test-workspace\node_modules\app-builder-bin\win\x64\app-builder.exe ENOENT

also it deletes more packages from node_modules, so next run throws

'electron-builder' is not recognized as an internal or external command

Reproduction: npm run build -w a in test-workspace.zip

ghost avatar Aug 31 '22 06:08 ghost

For me, i just get stuck at installing production dependencies but the behavior is the same as above.

However, it's working with electron-builder version 22 !

paulantoine2 avatar Sep 30 '22 11:09 paulantoine2

I am having the same or similar problem and found it started happening with npm 8.5.0 (node 16.14.1) because when I downgrade to 8.3.1 (that came with node 16.14.1) while keeping a higher node version, it works. Npm changelog says something about workspaces but I am not sure if its related.

radoslavkarlik avatar Oct 01 '22 09:10 radoslavkarlik

Try to add build option below in your package.json which has main entry for electron app:

{
  "main": "build/electron/main.js",
  ...,
  "build": {
    ...,
    "npmRebuild": false
  }
}

Hinaser avatar Jan 17 '23 16:01 Hinaser

Confirmed that setting npmRebuild to false works for my project, but I suspect it is not a universal workaround.

It looks like this code is where the installing production dependencies happens:

https://github.com/electron-userland/electron-builder/blob/01c679107435c6afd0b2de8c44d3f140d20c5577/packages/app-builder-lib/src/util/yarn.ts#L86-L120

It seems to be running node install --production. I'm guessing that this command is deleting dependencies needed by electron-builder, including the mentioned app-builder-bin.

joshtynjala avatar Feb 07 '23 22:02 joshtynjala

Setting npmRebuild to false works for me, but I worry disable rebuild may break native dependencies;

Another problem is when depend on a non-exact electron version like ^23.1.0, it can't find installed electron package and will throw.

It should use require.resolve() or require.resolve.paths() to search packages and not assume in {pwd}/node_modules

nanamicat avatar Feb 21 '23 14:02 nanamicat

I look into the code and figure out that create an empty node_module directory on the specific project where electron-builds solve my issue here. npm rebuild works fine in my case.

See https://github.com/MuhammedKalkan/OpenLens/blob/c4b099462f9e4cdf405d02993a40e5563278bfd1/.github/workflows/main.yml#L47-L49 for a living example.

electron-builder checks, if there is an node_modules dir on the current dir

https://github.com/electron-userland/electron-builder/blob/80a3ae857b9305aff1774e1b2b932601af50231b/packages/app-builder-lib/src/util/yarn.ts#L11-L16

and if not, it just runs npm install again which may not work in npm workspace scenarios.


An alternative solution is npmRebuild=false and postinstall: "electron-rebuild". This is, what this project is using, however it will not work in cross-compile scenarios.

jkroepke avatar Jun 10 '23 12:06 jkroepke

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Aug 10 '23 00:08 github-actions[bot]

still an issue

jkroepke avatar Aug 10 '23 05:08 jkroepke

Yes, still an issue: having to create an empty 'node_modules' folder on every workspace and having to fix the version of Electron on package.json is very annoying...

BrunoSoaresEngineering avatar Sep 19 '23 14:09 BrunoSoaresEngineering

Very intereesting.

Is there a proper way to detect if we're in a npm workspace and can discern that dependencies are already installed?

mmaietta avatar Sep 19 '23 22:09 mmaietta

Very intereesting.

Is there a proper way to detect if we're in a npm workspace and can discern that dependencies are already installed?

getElectronVersionFromInstalled(projectDir) is trying to implement its own package resolution algorithm which probably is not a good idea. A better solution could be:

cd ${projectDir}
node -p "require.resolve('electron')"

(…or node -p "import.meta.resolve('electron')" if project is a module.)

vincesp avatar Oct 02 '23 14:10 vincesp

I'm wondering if I could just copy this implementation. https://github.com/electron/rebuild/blob/main/src/electron-locator.ts

mmaietta avatar Oct 02 '23 18:10 mmaietta

It is important that the resolve() takes place in the context of the electron project, not in the context of the electron-builder itself.

vincesp avatar Oct 05 '23 10:10 vincesp

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Dec 22 '23 00:12 github-actions[bot]

not stale

rotu avatar Dec 22 '23 01:12 rotu

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Feb 21 '24 00:02 github-actions[bot]

still not stale

rotu avatar Feb 21 '24 01:02 rotu

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Apr 28 '24 00:04 github-actions[bot]

still not stale

rotu avatar Apr 28 '24 01:04 rotu

still not stale

vincesp avatar Apr 29 '24 07:04 vincesp