Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dynamic import not working inside asar #249

Closed
StreetStrider opened this issue Nov 28, 2022 · 5 comments
Closed

dynamic import not working inside asar #249

StreetStrider opened this issue Nov 28, 2022 · 5 comments

Comments

@StreetStrider
Copy link

I need to use esm only package (lowdb). To do this I use dynamic import to workaround electron's cjs-only. It works fine in dev build. After bundling app to asar this fails, because process cannot resolve import from lowdb. lowdb package featuring exports maps, so it might be related to the fact that required module file differs from import uri (resolved via export map).

@smhdfdl
Copy link

smhdfdl commented Feb 17, 2023

Also encountered this today. The specific line of code that fails only in the asar is...

    const { default: got } = await import('got')

@GitMurf
Copy link

GitMurf commented May 6, 2023

Check this out and the "Things that were fixed" section. This is a solution for ESM imports but also fixes I believe the problems with dynamic imports in asar. electron/electron#37535

@GitMurf
Copy link

GitMurf commented May 6, 2023

image

@glowtape
Copy link

How did you solve the issue with nested dependencies? I've tried an artifact of the pull request you referred, and while a lot of ESM things now work, it seems like it doesn't respect the exports section of the package.json files in the ASAR.

undergroundwires added a commit to undergroundwires/privacy.sexy that referenced this issue Aug 18, 2023
nklayman/vue-cli-plugin-electron-builder/issues/1622
electron/electron/issues/21457
electron/asar/issues/249

Fix desktop applications failing with following error:

```
A JavaScript error occurred in the main process
Uncaught Exception:
Error [ERR_REQUIRE_ESM]: require() of ES Module /tmp/.mount_privacSXvQfc/resources/app.asar/index.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 /tmp/.mount_privacSXvQfc/resources/app.asar/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at f._load (node:electron/js2c/asar_bundle:2:13330)
    at node:electron/js2c/browser_init:2:123492
    at node:electron/js2c/browser_init:2:123695
    at node:electron/js2c/browser_init:2:123699
    at f._load (node:electron/js2c/asar_bundle:2:13330)
```
undergroundwires added a commit to undergroundwires/privacy.sexy that referenced this issue Aug 19, 2023
nklayman/vue-cli-plugin-electron-builder/issues/1622
electron/electron/issues/21457
electron/asar/issues/249

Fix desktop applications failing with following error:

```
A JavaScript error occurred in the main process
Uncaught Exception:
Error [ERR_REQUIRE_ESM]: require() of ES Module /tmp/.mount_privacSXvQfc/resources/app.asar/index.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 /tmp/.mount_privacSXvQfc/resources/app.asar/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at f._load (node:electron/js2c/asar_bundle:2:13330)
    at node:electron/js2c/browser_init:2:123492
    at node:electron/js2c/browser_init:2:123695
    at node:electron/js2c/browser_init:2:123699
    at f._load (node:electron/js2c/asar_bundle:2:13330)
```
undergroundwires added a commit to undergroundwires/privacy.sexy that referenced this issue Aug 19, 2023
nklayman/vue-cli-plugin-electron-builder/issues/1622
electron/electron/issues/21457
electron/asar/issues/249

Fix desktop applications failing with following error:

```
A JavaScript error occurred in the main process
Uncaught Exception:
Error [ERR_REQUIRE_ESM]: require() of ES Module /tmp/.mount_privacSXvQfc/resources/app.asar/index.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 /tmp/.mount_privacSXvQfc/resources/app.asar/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at f._load (node:electron/js2c/asar_bundle:2:13330)
    at node:electron/js2c/browser_init:2:123492
    at node:electron/js2c/browser_init:2:123695
    at node:electron/js2c/browser_init:2:123699
    at f._load (node:electron/js2c/asar_bundle:2:13330)
```
undergroundwires added a commit to undergroundwires/privacy.sexy that referenced this issue Aug 19, 2023
nklayman/vue-cli-plugin-electron-builder/issues/1622
electron/electron/issues/21457
electron/asar/issues/249

Fix desktop applications failing with following error:

```
A JavaScript error occurred in the main process
Uncaught Exception:
Error [ERR_REQUIRE_ESM]: require() of ES Module /tmp/.mount_privacSXvQfc/resources/app.asar/index.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 /tmp/.mount_privacSXvQfc/resources/app.asar/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at f._load (node:electron/js2c/asar_bundle:2:13330)
    at node:electron/js2c/browser_init:2:123492
    at node:electron/js2c/browser_init:2:123695
    at node:electron/js2c/browser_init:2:123699
    at f._load (node:electron/js2c/asar_bundle:2:13330)
```
undergroundwires added a commit to undergroundwires/privacy.sexy that referenced this issue Aug 19, 2023
nklayman/vue-cli-plugin-electron-builder/issues/1622
electron/electron/issues/21457
electron/asar/issues/249

Fix desktop applications failing with following error:

```
A JavaScript error occurred in the main process
Uncaught Exception:
Error [ERR_REQUIRE_ESM]: require() of ES Module /tmp/.mount_privacSXvQfc/resources/app.asar/index.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 /tmp/.mount_privacSXvQfc/resources/app.asar/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at f._load (node:electron/js2c/asar_bundle:2:13330)
    at node:electron/js2c/browser_init:2:123492
    at node:electron/js2c/browser_init:2:123695
    at node:electron/js2c/browser_init:2:123699
    at f._load (node:electron/js2c/asar_bundle:2:13330)
```
undergroundwires added a commit to undergroundwires/privacy.sexy that referenced this issue Aug 19, 2023
nklayman/vue-cli-plugin-electron-builder/issues/1622
electron/electron/issues/21457
electron/asar/issues/249

Fix desktop applications failing with following error:

```
A JavaScript error occurred in the main process
Uncaught Exception:
Error [ERR_REQUIRE_ESM]: require() of ES Module /tmp/.mount_privacSXvQfc/resources/app.asar/index.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 /tmp/.mount_privacSXvQfc/resources/app.asar/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at f._load (node:electron/js2c/asar_bundle:2:13330)
    at node:electron/js2c/browser_init:2:123492
    at node:electron/js2c/browser_init:2:123695
    at node:electron/js2c/browser_init:2:123699
    at f._load (node:electron/js2c/asar_bundle:2:13330)
```
undergroundwires added a commit to undergroundwires/privacy.sexy that referenced this issue Aug 19, 2023
nklayman/vue-cli-plugin-electron-builder/issues/1622
electron/electron/issues/21457
electron/asar/issues/249

Fix desktop applications failing with following error:

```
A JavaScript error occurred in the main process
Uncaught Exception:
Error [ERR_REQUIRE_ESM]: require() of ES Module /tmp/.mount_privacSXvQfc/resources/app.asar/index.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 /tmp/.mount_privacSXvQfc/resources/app.asar/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at f._load (node:electron/js2c/asar_bundle:2:13330)
    at node:electron/js2c/browser_init:2:123492
    at node:electron/js2c/browser_init:2:123695
    at node:electron/js2c/browser_init:2:123699
    at f._load (node:electron/js2c/asar_bundle:2:13330)
```
undergroundwires added a commit to undergroundwires/privacy.sexy that referenced this issue Aug 19, 2023
nklayman/vue-cli-plugin-electron-builder/issues/1622
electron/electron/issues/21457
electron/asar/issues/249

Fix desktop applications failing with following error:

```
A JavaScript error occurred in the main process
Uncaught Exception:
Error [ERR_REQUIRE_ESM]: require() of ES Module /tmp/.mount_privacSXvQfc/resources/app.asar/index.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 /tmp/.mount_privacSXvQfc/resources/app.asar/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at f._load (node:electron/js2c/asar_bundle:2:13330)
    at node:electron/js2c/browser_init:2:123492
    at node:electron/js2c/browser_init:2:123695
    at node:electron/js2c/browser_init:2:123699
    at f._load (node:electron/js2c/asar_bundle:2:13330)
```
undergroundwires added a commit to undergroundwires/privacy.sexy that referenced this issue Aug 19, 2023
nklayman/vue-cli-plugin-electron-builder/issues/1622
electron/electron/issues/21457
electron/asar/issues/249

Fix desktop applications failing with following error:

```
A JavaScript error occurred in the main process
Uncaught Exception:
Error [ERR_REQUIRE_ESM]: require() of ES Module /tmp/.mount_privacSXvQfc/resources/app.asar/index.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 /tmp/.mount_privacSXvQfc/resources/app.asar/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at f._load (node:electron/js2c/asar_bundle:2:13330)
    at node:electron/js2c/browser_init:2:123492
    at node:electron/js2c/browser_init:2:123695
    at node:electron/js2c/browser_init:2:123699
    at f._load (node:electron/js2c/asar_bundle:2:13330)
```
undergroundwires added a commit to undergroundwires/privacy.sexy that referenced this issue Aug 24, 2023
- Switch from deprecated Vue CLI plugin to `electron-vite` (see
  nklayman/vue-cli-plugin-electron-builder#1982)
- Update main/preload scripts to use `index.cjs` filenames to support
  `"type": "module"`, resolving crash issue (#233). This crash was
  related to Electron not supporting ESM (see electron/asar#249,
  electron/electron#21457).
- This commit completes migration to Vite from Vue CLI (#230).

Structure changes:

- Introduce separate folders for Electron's main and preload processes.
- Move TypeHelpers to `src/` to mark tit as accessible by the rest of
  the code.

Config changes:

- Make `vite.config.ts` reusable by Electron configuration.
- On electron-builder, use `--publish` flag instead of `-p` for clarity.

Tests:

- Add log for preload script loading verification.
- Implement runtime environment sanity checks.
- Enhance logging in `check-desktop-runtime-errors`.
@erikian
Copy link
Member

erikian commented Oct 16, 2023

Should be fixed by electron/electron#40221. I'm closing this for now since the issue is not on @electron/asar's side.

@erikian erikian closed this as completed Oct 16, 2023
LarrMarburger added a commit to LarrMarburger/privacy.sexy that referenced this issue Nov 16, 2023
- Switch from deprecated Vue CLI plugin to `electron-vite` (see
  nklayman/vue-cli-plugin-electron-builder#1982)
- Update main/preload scripts to use `index.cjs` filenames to support
  `"type": "module"`, resolving crash issue (#233). This crash was
  related to Electron not supporting ESM (see electron/asar#249,
  electron/electron#21457).
- This commit completes migration to Vite from Vue CLI (#230).

Structure changes:

- Introduce separate folders for Electron's main and preload processes.
- Move TypeHelpers to `src/` to mark tit as accessible by the rest of
  the code.

Config changes:

- Make `vite.config.ts` reusable by Electron configuration.
- On electron-builder, use `--publish` flag instead of `-p` for clarity.

Tests:

- Add log for preload script loading verification.
- Implement runtime environment sanity checks.
- Enhance logging in `check-desktop-runtime-errors`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants