electron / electron/forge

"type": "module" in package.json for Vite templates

Open
#3,502 14 comments 15 reactions 0 assignees View on GitHub
template/vite-typescript
Dominant language
TypeScript
Stars
7.1k
Forks
641
Avg merge
3d 1h
Merged PRs (30d)
30

Description

### Pre-flight checklist

- [X] I have read the [contribution documentation](https://github.com/electron/forge/blob/main/CONTRIBUTING.md) for this project.
- [X] I agree to follow the [code of conduct](https://github.com/electron/electron/blob/main/CODE_OF_CONDUCT.md) that this project uses.
- [X] I have searched the issue tracker for a bug that matches the one I want to file, without success.

### Electron Forge version

7.3.0

### Electron version

29.0.1

### Operating system

macOS 14.3.1

### Last known working Electron Forge version

_No response_

### Expected behavior

I wanted to try out the newly stable `@remix-run/dev` Vite plugin in combination with `electron-forge`. I initialized a new project with the `create-electron-app` `vite-typescript` template, then started layering on pieces like `dev` and `build` scripts in `package.json`.

### Actual behavior

I got `remix vite:dev` working with an isolated `vite.remix.config.ts`, but then `remix vite:build` ran into `SyntaxError: Cannot use import statement outside a module`. Adding `"type": "module"` to `my-app`'s `package.json` seemed to fix the problem, but then `electron-forge` scripts started failing.

### Steps to reproduce

For `vite-typescript` template:
- `npm init electron-app@7.3.0 my-app -- --template=vite-typescript`
- Add `"type": "module"` to `package.json`.
- `npm start`
```
An unhandled rejection has occurred inside Forge:
Error: Must use import to load ES Module: /my-app/forge.config.ts
require() of ES modules is not supported.
require() of /my-app/forge.config.ts from /my-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 /my-app/package.json
```

For `vite` template:
- `npm init electron-app@7.3.0 my-app -- --template=vite`
- Add `"type": "module"` to `package.json`.
- `npm start`
```
An unhandled rejection has occurred inside Forge:
Error [ERR_REQUIRE_ESM]: require() of ES Module /my-app/forge.config.js from /my-app/node_modules/@electron-forge/core/dist/util/forge-config.js not supported.
forge.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 forge.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 /my-app/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).
at exports.default (/my-app/node_modules/@electron-forge/core/dist/util/forge-config.js:140:26)
at async /my-app/node_modules/@electron-forge/core/dist/api/start.js:44:35
at async Task.task (/my-app/node_modules/@electron-forge/tracer/dist/index.js:58:20)
at async Task.run (/my-app/node_modules/listr2/dist/index.cjs:978:11)
at async /my-app/node_modules/p-map/index.js:57:22
```

### Additional information

Seems related to https://github.com/electron/forge/issues/3439, and does not appear to be solved with https://github.com/electron/forge/pull/3468

This is likely causing friction still with SvelteKit as well.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.