alex8088 / quick-start

An easy way to start a front-end project.
MIT License
248 stars 34 forks source link

ESM module type project error #7

Closed adminy closed 1 year ago

adminy commented 1 year ago

Describe the bug

package.json

{
"type": "module",
"main": "out/main/index.js"
}
npm start

> test_project@1.0.0 start
> electron-vite dev --watch

vite v3.2.5 building for development...

watching for file changes...

build started...
✓ 18 modules transformed.
out/main/index.js   181.97 KiB
built in 330ms.

build the electron main process successfully

-----

vite v3.2.5 building for development...

watching for file changes...

build started...
✓ 5 modules transformed.
out/preload/index.js   2.53 KiB
built in 13ms.

build the electron preload files successfully

-----

dev server running for the electron renderer process at:

  ➜  Local:   http://localhost:5173/
  ➜  Network: use --host to expose

start electron app...
App threw an error during load

Error [ERR_REQUIRE_ESM]: require() of ES Module /project/out/main/index.js from /project/node_modules/electron/dist/Electron.app/Contents/Resources/default_app.asar/main.js not supported.
index.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 rename index.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 /project/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at c._load (node:electron/js2c/asar_bundle:5:13339)
    at loadApplicationPackage (/project/node_modules/electron/dist/Electron.app/Contents/Resources/default_app.asar/main.js:121:16)
    at Object.<anonymous> (/project/node_modules/electron/dist/Electron.app/Contents/Resources/default_app.asar/main.js:233:9)
    at c._load (node:electron/js2c/asar_bundle:5:13339)
    at Object.<anonymous> (node:electron/js2c/browser_init:189:3102)
    at ./lib/browser/init.ts (node:electron/js2c/browser_init:189:3306)
    at __webpack_require__ (node:electron/js2c/browser_init:1:128)
    at node:electron/js2c/browser_init:1:1200
    at node:electron/js2c/browser_init:1:1267
    at c._load (node:electron/js2c/asar_bundle:5:13339)

I would like to use esm and not commonjs.

Removing the type: module from package.json solves the issue but causes other things to fail, and I'd like to keep type: module as is.

Used Scaffolding

create-electron

Used Package Manager

npm

Validations

alex8088 commented 1 year ago

electron not support "type": "module",need to remove

adminy commented 1 year ago

so its related to https://github.com/electron/electron/issues/21457

any workaround? I really need to have type: module.

alex8088 commented 1 year ago

As far as I know, there is no solution.

wmelton commented 1 year ago

Difficult to imagine a future for the Electron project where ESM and ES6+ as a whole aren't first-class citizens.

alex8088 commented 1 year ago

This issue will be closed as there is nothing to do. If you have any other questions, please feel free to submit an issue.

wmelton commented 1 year ago

Yeah no I get it. Do you know any maintainers of the Electron project we could loop in to the discussion though? Cool if not, just curious.