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

Electron 29 beta Cannot Find Packages when using ESM #401

Closed
4 tasks done
johnlindquist opened this issue Feb 3, 2024 · 8 comments
Closed
4 tasks done

Electron 29 beta Cannot Find Packages when using ESM #401

johnlindquist opened this issue Feb 3, 2024 · 8 comments

Comments

@johnlindquist
Copy link

Describe the bug

Reproduce

You can use this repo to reproduce (it just followed the steps below): https://github.com/johnlindquist/electron-29

npm run dev

Works fine

npm run build:mac

  • Successfully builds
  • open ./dist/mac-arm64/foo.app

Your run into a Cannot find package, "electron-log" error.

Uncaught Exception:
Error: Cannot find package '/Users/johnlindquist/dev/electron-29/dist/mac-arm64/electron-29.app/Contents/Resources/app.asar/node_modules/electron-log/package.json' imported from /Users/johnlindquist/dev/electron-29/dist/mac-arm64/electron-29.app/Contents/Resources/app.asar/out/main/index.mjs
Did you mean to import electron-log/src/index.js?
at legacyMainResolve (node:internal/modules/esm/resolve:189:26)
at packageResolve (node:internal/modules/esm/resolve:783:14)
at moduleResolve (node:internal/modules/esm/resolve:845:20)
at defaultResolve (node:internal/modules/esm/resolve:1050:11)
at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:383:12)
at ModuleLoader.resolve (node:internal/modules/esm/loader:352:25)
at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:228:38)
at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:85:39)
at link (node:internal/modules/esm/module_job:84:36)

CleanShot 2024-02-03 at 09 41 14

Fix

Downgrade to Electron 28, build works fine again.

Electron-Vite Version

2.0.0

Electron Version

29.0.0-beta.5

Vite Version

5.0.11

Validations

@1zilc
Copy link

1zilc commented Feb 5, 2024

我前几天也遇到过,当时以为是external的问题,结果asar:false 后node_modules 都在里面,并且能正常运行 ,这个应该是electron 29的问题,建议反馈到electron issue

@undergroundwires
Copy link

undergroundwires commented Mar 14, 2024

I still reproduce this with latest electron-vite (2.1.0), electron-builder (24.13.3), electron-log (v5.1.2) and electron (v29.1.4 same with latest v28, i.e. v28.2.7).

The suggested solutions did not work for me:

  • Downgrading Electron to v28 did not solve my issue, the same error latest v28 (v28.2.7).
  • Setting asar: false had no affect.

Error is there when running electron-vite dev and electron-vite preview.

How did you solve this?

@1zilc
Copy link

1zilc commented Mar 15, 2024

I still reproduce this with latest electron-vite (2.1.0), electron-builder (24.13.3), electron-log (v5.1.2) and electron (v29.1.4 same with latest v28, i.e. v28.2.7).

The suggested solutions did not work for me:

  • Downgrading Electron to v28 did not solve my issue, the same error latest v28 (v28.2.7).

  • Setting asar: false had no affect.

Error is there when running electron-vite dev and electron-vite preview.

How did you solve this?

try 29.1.1

@undergroundwires
Copy link

I got same error running after clean install of Electron 29.1.1. I run electron-vite build before running dev just in case.

Error:

Uncaught Exception:
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/bob/user/dev/undergroundwires/privacy.sexy/node_modules/electron-log/main' imported from /home/bob/user/dev/undergroundwires/privacy.sexy/dist-electron-unbundled/main/index.mjs
Did you mean to import electron-log/main.js?
    at new NodeError (node:internal/errors:406:5)
    ...

I realize that I'm only getting this error if I import electron-log from /main alias such as:

import log from "electron-log/main";

Importing only from electron-log does not yield this error:

import log from "electron-log";

However importing it as /main is recommended in its README as it's best practice to get right context.

I'm unsure if this has anything to do with electron-vite or electron/electron#41241.

@alex8088
Copy link
Owner

alex8088 commented Mar 18, 2024

electron-vite only builds the code and does not affect the runtime. This problem may be a problem with electron-builder or electron. There is no problem with subpath modules under cjs, so it is more likely to be an electron issue. The PR electron/electron#41241. does not seem to solve all the problems. This looks like an asar problem, electron esm loader cannot correctly resolve subpath modules.

@alex8088
Copy link
Owner

Don't externalize the module yet and let electron-vite bundle it. This is also a way to avoid this problem.

@undergroundwires
Copy link

The suggested workaround worked for me. Thank you for the capability, sticking around and supporting the community @alex8088 ❤️.

This fixes it:

externalizeDepsPlugin({
      exclude: [ electron-log' ]
})]

undergroundwires added a commit to undergroundwires/privacy.sexy that referenced this issue Mar 18, 2024
This commit bumps Electron and related dependencies to their latest
versions to leverage native ESM support. It adjusts build configuration
to use native ESM support instead of relying on CommonJS bundling.

Key changes:

- Bump Electron to latest v29.
  Electron v28 ships with native ESM/ECMAScript modules support.
  Details on Electron ESM support:
    - electron/electron#21457
    - electron/electron#37535
- Bump `electron-builder` to latest v24.13.
  `electron-builder` is used to package and publish the application.
  It supports ESM since 24.10.
  Details on `electron-builder` ESM support:
    - electron-userland/electron-builder#7936
    - electron-userland/electron-builder#7935
- Bump `electron-log` to latest v5.1.
  `electron-log` supports ESM since version 5.0.4.
  Details on `electron-log` ESM support:
    - megahertz/electron-log#390.
- Change `electron-vite` configuration to bundle as ESM instead of
  CommonJS to leverage Electron's native ESM support.

Other supporting changes:

- Add type hint for electron-builder configuration file.
- Update import statements for `electron-updater` as it still is a
  CommonJS module and does not support ESM.
  Details:
    - electron-userland/electron-builder#7976
- Improve `electron-builder` configuration file to dynamically locate
  main entry files, supporting various JavaScript file extensions
  (`.js`, `.mjs` and `.cjs`) to facilitate easier future changes.
- Change comment about Electron process-specific module alias
  registration. This issue has been fixed in `electron-vite`, but
  subpath module imports for Electron still do not work when building
  tests (`npm run test:unit`).
  Details:
   - alex8088/electron-vite#372
- Add `electron-log` in bundling process instead of externalizing to
  workaround Electron ESM loader issues with subpath imports (inability
  to do `electron-log/main`).
  Details:
    - alex8088/electron-vite#401
    - electron/electron#41241
- Improve desktop runtime error checks' assertion message for better
  clarity.
@theogravity
Copy link

I was able to get around this with import log from "electron-log/main.js";

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