-
Notifications
You must be signed in to change notification settings - Fork 15.7k
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
Support Node's ES Modules #21457
Comments
FWIW, Electron won't be supporting Node 13 as our policy is to only roll in LTS versions of Node. So, while we definitely want to support ESM, this isn't going to be happening until Node 14. |
Sure, following LTS makes sense for production releases, but there's no harm in creating a v10 branch and adding ESM support now with Node v13 which can be rolled over to v14 once it's out in April 2020. |
@pauliusuza "no harm" indeed, except that we would then have to make sure we backport every change from master into the v10 branch, which is a significant burden on the maintainer team. We'll do like we've done for all previous release branches, and branch v10 once v9 is stable. See https://electronjs.org/docs/tutorial/electron-timelines. |
As a workaround, you can use the Then, once Node 14 is out and Electron upgrades, all you need to do is modify the entry point not to import |
That got the job done, Thanks! @trusktr |
wish electron use deno replace node |
@nornagon ES modules unflagged is expected to land in April for Node 12. For more details, see nodejs/modules#450. |
I don't quite undestand how you did this. Can you give an example of how this workaround is supposed to work? Edit: nevermind, I got it working. My code looks like this now (if anyone's wondering):
with my Electron (main process) startup code in |
Node 12.17 removed necessity to enable -experimental-modules flag, so I wonder if after incorporating #23789 it will just start working out of the box. And which electron release will get it... :) |
It is [email protected] already, with stabilized support for ESM. It is time for the ecosystem to gradually switch to common standard. Would be really nice if electron enabled modules. esm doesn't work with node@13+. |
As mentioned earlier in the thread, we will support this when Electron includes a version of node which supports it, and not before. Electron only includes LTS releases of Node, and does not upgrade Node versions in stable releases. Node 14 won't be LTS until October, so the earliest version of Electron that could include Node 14 LTS is Electron 12. |
@nornagon Node 12 LTS already supports it. See the Node release notes: |
Ah, in that case Electron 11 will likely include support for it. |
@nornagon I would expect some stuff to break without deeper testing of native ESM with Electron's API. Will the entire Electron API be supported or parts of it in ESM in Electron 11? |
Full transparency we might turn off node's ESM loader in Electron 11 and it will almost certainly be disabled completely in renderer processes. Still reviewing exactly what the implications of this loader, it's implementation and it's interactions with Chromium exactly are and when we know more we'll share it but I want to set the expectations quite clearly here. This is not a simple flag or switch we can just turn on and move on with our lives, there are significant security, performance and compatibility concerns that we need to address here. |
@MarshallOfSound @nornagon While I understand the challenge of integrating Node and Chromium on a periodic basis, integrating ESM into Electron will be a challenge in itself. I believe the spirit of this current issue is to sort out these security, performance and compatibility issues before ESM lands. Maybe it is impossible to do beforehand. Any ideas on how to proceed? How should this be tackled? |
Frankly, that's exactly the opposite of what I expected to hear. |
@pauliusuza Weback already converts all your imports into one file, so you can write things like |
Latest version of Electron According to https://nodejs.org/en/about/releases/ Node v14 will be LTS at the end of October If we get ESM module in Electron before Christmas it would be a great deal, but we shouldn't hold our breaths until then. |
@cata-code If there is such a precedent, I'm not convinced native ESM would land with 14.0.0 in Electron. There is much work to verify everything beforehand and no plan that I'm aware of. Please take note of the comment history in this issue. |
Ok, so instead of waiting for this feature, we can use imports in electron main files with the help of webpack. This is how I set it up for my app, using webpack for both client side and electron main files webpack.config.cjs
index.js
Foo.js
|
I was using solution mentioned by @leontepe for quite some time and it allows me to use esm modules and imports without problems by using "esm" node module and this little wrapper (main_esm.js) for entrypoint (main.js):
ironically at runtime Electron complains that using require in esm modules, which apparently it thinks this file is (sic!), isn't supported:
Hope that whatever version of Electron that will be branched from current master (which already includes #23789 forcing support of esm in node AFAICT) will allow me to remove above little hack. |
@kskalski I had a look at the source code for the I rather use webpack and compile it once, and then the app is optimized every time you run it. Besides you are most likely using webpack for the client side code. So when webpack is run, it builds both main and render/client files. |
When Electron upgrades to using Node 14, would I be able to do this: my-app/package.json my-app/index.js And then run: Would I also be able to use Node's Would love this. How can I help? |
- 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`.
There's a rather popular project that uses TypeScript and WebPack to build Electron apps with, which uses Would anyone be able to provide support or guide towards a fix? The specific issue seems to be only with Node v18.19.0 or above, and Electron v28. |
Well it doesn't "load" typescript sources at runtime, it resolves them in typescript, stores them somewhere and then redirects |
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.
So, I'm trying Electron 30 with the following : import { app, BrowserWindow } from 'electron';
await app.whenReady();
const mainWindow = new BrowserWindow({
width: 800,
height: 600
});
await mainWindow.loadURL('https://example.com'); The commands runs indefinitely but nothing happens 🤔 What am I missing ? Thanks |
Indeed, thanks ! It would be nice to update the docs, especially the quick start, to include ESM. |
So, preload doesn't support ESM even with the |
@KaKi87 Yes, if you print a log from the sandboxed preload script, you will find that the script is wrapped in an IIFE like this: (function(require, process, Buffer, global, setImmediate, clearImmediate, exports, module) {
// preload.js contents
}) So it seems the preload script is always executed in IIFE format with sandboxing enabled. |
Is static import support planned ? Meanwhile, could the dynamic import function be added to these ? |
@KaKi87 from my experience, it is better to just bundle everything (with the exception of |
Important
Support for ES Modules in Electron will land in Electron 28 and will be available in the
electron-nightly
released on August 31st🥳
Original Issue Below
Preflight Checklist
Feature Request
As of Node 13.3 ES modules have been unflagged (albeit still experimental), it would be nice to be able to use ES modules as supported in electron.
Currently when trying to run electron with an ES entry point (either
.mjs
or.js
+"type": "module"
) electron fails to run.For example:
Alternatives Considered
This is primarily a request for support, the alternative is not supporting it but that would be unfortunate given ES modules will be officially supported in both browsers and Node but not usable by electron without additional tooling.
Additional Information
ES Modules are still new (and experimental) so actual implementation might be delayed (or put behind flag) however starting to consider implementation considerations now would be helpful for supporting this more readily in future.
The text was updated successfully, but these errors were encountered: