forge: Typescript ESM forge.config.ts error
Pre-flight checklist
- I have read the contribution documentation for this project.
- I agree to follow the code of conduct that this project uses.
- I have searched the issue tracker for a bug that matches the one I want to file, without success.
Electron Forge version
7.4.0
Electron version
v31.3.1
Operating system
Windows 11
Last known working Electron Forge version
No response
Expected behavior
To not get an error when using a Typescript ESM forge.config.ts.
Actual behavior
Error are listed under the steps to reproduce section.
Steps to reproduce
Run npm init electron-app@7.4.0 my-new-app -- --template=vite-typescript
Add "type": "module", to package.json
[!Note]
To make other parts of the template work with "type": "module"
In
vite.main.configchangeformats: ['cjs'],toformats: ['es'],In
vite.preload.configchangeformat: 'cjs',toformat: 'es',In
src/main.tschangeif (require('electron-squirrel-startup')) { app.quit(); } ↓↓↓ import electronSquirrelStartup from 'electron-squirrel-startup'; if(electronSquirrelStartup) app.quit();and replace
__dirnamewithimport.meta.dirnameYou can also
npm install --save-dev @types/electron-squirrel-startupand intsconfig.jsonchange"module": "commonjs",→"module": "ESNext",to make typescript happy.
Run npm run start
This result in this error…
› Must use import to load ES Module: C:\path\my-new-app\forge.config.ts
require() of ES modules is not supported.
require() of C:\path\my-new-app\forge.config.ts from C:\path\my-new-app\node_modules\@electron-forge\core\dist\util\forge-config.js is an ES
module file as it is a .ts file whose nearest parent package.json contains "type": "module" which defines all .ts
files in that package scope as ES modules.
Instead change the requiring code to use import(), or remove "type": "module" from
C:\path\my-new-app\package.json.
An unhandled rejection has occurred inside Forge:
Error: Must use import to load ES Module: C:\path\my-new-app\forge.config.ts
require() of ES modules is not supported.
require() of C:\path\my-new-app\forge.config.ts from C:\path\my-new-app\node_modules\@electron-forge\core\dist\util\forge-config.js is an ES module file as it is a .ts file whose nearest parent package.json contains "type": "module" which defines all .ts files in that package scope as ES modules.
Instead change the requiring code to use import(), or remove "type": "module" from C:\path\my-new-app\package.json.
However changing forge.config.ts to forge.config.js (and removing the type definitions) makes the error go away. So seemingly JS ES modules work but TS ES modules do not work.
Another option is changing the forge.config.ts to a forge.config.cts but it does not work with "module": "ESNext" (or other ES module options) in the tsconfig.json which gives the error…
› Cannot use import statement outside a module
Failed to load: C:\path\my-new-app\forge.config.cts
An unhandled rejection has occurred inside Forge:
SyntaxError: Cannot use import statement outside a module
Additional information
No response
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 9
- Comments: 16 (3 by maintainers)
Commits related to this issue
- Add workaround for typescript config file. See https://github.com/electron/forge/issues/3671#issuecomment-2531935206 — committed to Capricom/Delta by Capricom 2 years ago
- :wrench: Add workaround for node 23 (see https://github.com/electron/forge/issues/3671#issuecomment-2531935206) — committed to kando-menu/kando by Schneegans 2 years ago
A work-around I found is calling the Forge cli command script’s with something like
tsx. So in yourpackage.jsonadd this…I am running into the issue as well but with webpack. Anyone else have this issue?
Steps to reproduce
Run npm init electron-app@latest my-new-app – --template=webpack-typescript
Add “type”: “module”, to package.json
Error
Failed to load: electron-test/issue-3671/forge.config.js
An unhandled rejection has occurred inside Forge: Error [ERR_REQUIRE_ESM]:
I found that
rechoirsupportsesbuild. You can removets-nodefrom your devDependencies and replace it withesbuild-registerand then forge.config.ts can be loaded successfully.With Yarn:
I also encountered this problem. When I wanted to migrate to ESM according to the latest version of electron-store, I found that forge.config.ts did not support ESM.
@codec-xyz I am just glad I am not crazy and that you came to the same conclusion as me! I spent way too many hours (days) trying to figure this out and the imposter syndrome was starting to become real strong for me! haha
Using the template shared in the issue below worked for me initially, But later I needed to change the preload script output to cjs and moduleResolution to Node because some issues started showing up, Not sure if it was a problem on my end but with those changes everything seem to work fine.
https://github.com/electron/forge/issues/3502#issuecomment-2211449451