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

Errors when running in ESM #390

Closed
Stanzilla opened this issue Dec 30, 2023 · 3 comments
Closed

Errors when running in ESM #390

Stanzilla opened this issue Dec 30, 2023 · 3 comments

Comments

@Stanzilla
Copy link

Just started moving my app to electron 28 with ESM and getting these errors from electron-log

07:47:45.068 › ReferenceError: __dirname is not defined
    at initializePreload (file:///Users/stan/projects/personal/WeakAuras-Companion/dist-electron/main/index.mjs:324:88)
    at file:///Users/stan/projects/personal/WeakAuras-Companion/dist-electron/main/index.mjs:304:11
    at Object.onAppReady (file:///Users/stan/projects/personal/WeakAuras-Companion/dist-electron/main/index.mjs:81:7)
    at _Logger.initialize [as initializeFn] (file:///Users/stan/projects/personal/WeakAuras-Companion/dist-electron/main/index.mjs:301:19)
    at _Logger.initialize (file:///Users/stan/projects/personal/WeakAuras-Companion/dist-electron/main/index.mjs:1393:10)
    at file:///Users/stan/projects/personal/WeakAuras-Companion/dist-electron/main/index.mjs:29800:5
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)
Unhandled electron-log error ReferenceError: require is not defined
    at Object.readPackageJson (file:///Users/stan/projects/personal/WeakAuras-Companion/dist-electron/main/index.mjs:940:24)
    at getNameAndVersion (file:///Users/stan/projects/personal/WeakAuras-Companion/dist-electron/main/index.mjs:1047:37)
    at Object.getPathVariables (file:///Users/stan/projects/personal/WeakAuras-Companion/dist-electron/main/index.mjs:1060:26)
    at initializeOnFirstAccess (file:///Users/stan/projects/personal/WeakAuras-Companion/dist-electron/main/index.mjs:1145:21)
    at getFile (file:///Users/stan/projects/personal/WeakAuras-Companion/dist-electron/main/index.mjs:1172:5)
    at transport (file:///Users/stan/projects/personal/WeakAuras-Companion/dist-electron/main/index.mjs:1128:19)
    at _Logger.processMessage (file:///Users/stan/projects/personal/WeakAuras-Companion/dist-electron/main/index.mjs:1432:11)
    at _Logger.logData (file:///Users/stan/projects/personal/WeakAuras-Companion/dist-electron/main/index.mjs:1396:10)
    at <computed> [as warn] (file:///Users/stan/projects/personal/WeakAuras-Companion/dist-electron/main/index.mjs:1358:37)
    at file:///Users/stan/projects/personal/WeakAuras-Companion/dist-electron/main/index.mjs:314:16
07:47:45.072 › App starting...
Unhandled electron-log error ReferenceError: require is not defined
    at Object.readPackageJson (file:///Users/stan/projects/personal/WeakAuras-Companion/dist-electron/main/index.mjs:940:24)
    at getNameAndVersion (file:///Users/stan/projects/personal/WeakAuras-Companion/dist-electron/main/index.mjs:1047:37)
    at Object.getPathVariables (file:///Users/stan/projects/personal/WeakAuras-Companion/dist-electron/main/index.mjs:1060:26)
    at initializeOnFirstAccess (file:///Users/stan/projects/personal/WeakAuras-Companion/dist-electron/main/index.mjs:1145:21)
    at getFile (file:///Users/stan/projects/personal/WeakAuras-Companion/dist-electron/main/index.mjs:1172:5)
    at transport (file:///Users/stan/projects/personal/WeakAuras-Companion/dist-electron/main/index.mjs:1128:19)
    at _Logger.processMessage (file:///Users/stan/projects/personal/WeakAuras-Companion/dist-electron/main/index.mjs:1432:11)
    at _Logger.logData (file:///Users/stan/projects/personal/WeakAuras-Companion/dist-electron/main/index.mjs:1396:10)
    at <computed> [as info] (file:///Users/stan/projects/personal/WeakAuras-Companion/dist-electron/main/index.mjs:1358:37)
    at file:///Users/stan/projects/personal/WeakAuras-Companion/dist-electron/main/index.mjs:29801:5
2023-12-30 07:47:45.096 Electron[41484:2802401] WARNING: Secure coding is automatically enabled for restorable state! However, not on all supported macOS versions of this application. Opt-in to secure coding explicitly by implementing NSApplicationDelegate.applicationSupportsSecureRestorableState:.

Repo/branch would be here WeakAuras/WeakAuras-Companion#2257

@megahertz
Copy link
Owner

I still need to test this lib for the case when it's bundled to ESM. I'm going to fix that in January. In your case, you can move electron-log to normal dependencies as a workaround.

@megahertz
Copy link
Owner

I tried to add a workaround for Vite. You can try electron-log@5.0.5-beta.2

@Stanzilla
Copy link
Author

I tried to add a workaround for Vite. You can try electron-log@5.0.5-beta.2

thanks! looks like no errors so far

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.
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

2 participants