electron-builder: ESM Config filetype causes import issues in external dep (pkg read-config-file)
- Electron-Builder Version: 24.13.3
- Node Version: 20.12.2
- Electron Version: 31.3.1
- Electron Type (current, beta, nightly): current
- Electron Updater Version: 5.2.1
- Target: ANY
The current external dependency used to load the config file is not prepared to handle ESM and therefore breaks the build if using native ESM modules and an external config (ESM js file).
util/config.ts calls to --> read-config-file-main.ts#L19 to load the config file which causes an error as there are no checks if we passed an ESM module type *.js file and does not use dynamic imports to load the file.
How to reproduce.
- Create a vanilla project with an external config and electron-builder installed
- add
type: "module"to package.json - Run
yarn run electron-builder build -c ./PATH_TO_ESM_JS_FILEto start build
The following error is presented:
❯ yarn run electron-builder build -c ./electron-builder.config.js ✗ staging
yarn run v1.22.21
$ /home/{{PATH_REDACTED}}/node_modules/.bin/electron-builder build -c ./electron-builder.config.js
• electron-builder version=24.13.3 os=6.5.0-44-generic
⨯ require() of ES Module /home/{{PATH_REDACTED}}/electron-builder.config.js from /home/{{PATH_REDACTED}}/node_modules/read-config-file/out/main.js not supported.
electron-builder.config.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead either rename electron-builder.config.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /home/{{PATH_REDACTED}}/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).
failedTask=build stackTrace=Error [ERR_REQUIRE_ESM]: require() of ES Module /home/{{PATH_REDACTED}}/electron-builder.config.js from /home/{{PATH_REDACTED}}p/node_modules/read-config-file/out/main.js not supported.
electron-builder.config.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead either rename electron-builder.config.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /home/{{PATH_REDACTED}}/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 18 (9 by maintainers)
@mmaietta I’ll have an opportunity to test again tomorrow morning. I’ll use the DEBUG env flag to see if any other stuff spits out and report back. It may just be my existing setup that I’m testing it in. I will have time tomorrow to use a vanilla base project to sanity check that theory as well.
@jbool24 please try 25.0.6 with
DEBUG=electron-builderin case any debug messages are thrownHmmm, the TS compilation shouldn’t impact the loading of the config file unless you’re also compiling the electron-builder config file prior to electron-builder loading it.
In any case, I was able to test successfully for both electron-builder configs with (.ts, .cjs, .js, and .mjs using
module.exportsorexport defaultdepending on type=module or not) and the CI tests are still passing.Just pushed the release commit, so a new deployment for 25.0.6 should occur in the next 10min.
@mmaietta Maybe, hold off and leave that to each user case to monkey patch locally. This library might be updated soon, if not by them I’ll submit a PR. No need for increasing workload 😉
I originally was going to use the builder API directly to get around the loading but got things to work except kinda weirdly with different file types and explicit file extension naming. For now I’d say pause until it blocks more people proving in needs to change.
@mmaietta Hi Mike! I am documenting this here so that I can link it to a ticket for the upstream lib (I’m going to create one in read-config-file). I’ve found a bunch of people using electron-builder and slamming heads into tables because the docs made it seem like it should load an ESM based config file. But the required lib (read-config-file specifically) is not ESM friendly currently.