electron: ESM Modules loading in main crash with unsupported url scheme

Preflight Checklist

Electron Version

35.1.5

What operating system(s) are you using?

Ubuntu

Operating System Version

Ubuntu 24.04.2

What arch are you using?

x64

Last Known Working Electron version

No response

Expected Behavior

ESM modules should load even when not bundler (such as webpack) in packaged apps as they do when not packaged.

Actual Behavior

When a packaged application using commonjs that is not bundled attempts to load a ESM module that is in the asar file Electron crashes with an ERR_UNSUPPORTED_ESM_URL_SCHEME error in the main process. Note this only seems to affect packaged applications, when the application is not packaged it loads fine. I’ve also experienced this on Windows 10 as well so it doesn’t seem to be just isolated to Ubuntu 24.04.

Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only URLs with a scheme in: file and data are supported by the default ESM loader. Received protocol 'electron:'
    at getSourceSync (node:internal/modules/esm/load:74:11)
    at defaultLoadSync (node:internal/modules/esm/load:172:32)
    at ModuleLoader.getModuleJobForRequire (node:internal/modules/esm/loader:410:24)
    at new ModuleJobSync (node:internal/modules/esm/module_job:341:34)
    at ModuleLoader.importSyncForRequire (node:internal/modules/esm/loader:357:11)
    at loadESMFromCJS (node:internal/modules/cjs/loader:1392:24)
    at Module._compile (node:internal/modules/cjs/loader:1543:5)
    at Module._extensions..js (node:internal/modules/cjs/loader:1722:10)
    at Module.load (node:internal/modules/cjs/loader:1296:32)
    at Module._load (node:internal/modules/cjs/loader:1115:12)

Testcase Gist URL

https://gist.github.com/b2e85840f148bd1e0b5eedae1d32ef86

Additional Information

This is potentially a similar issue to that of #46448.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 3
  • Comments: 18 (4 by maintainers)

Most upvoted comments

It seems to be anything that imports electron’s apis in ESM when you load it though require in commonjs. My stripped down gist above just does that and it throws an error.

Hi all; thanks for your work documenting this. I’m experiencing it as well and am curious: do we know of a stable previous version number for electron that does not have this issue? If so I could use that in the meantime. So far I’ve tried back to 35.0.3 with no luck. I’ll report back if I find anything further.

I also wanted to confirm that I get this error regardless of that status of ASAR with electron versions from 35.1.5 back to 35.0.3 (and possibly beyond, still need to test).

Thanks for reporting!

This doesn’t look like the same issue as #46448 because you import from the main thread while #46448 is about importing from the renderer.

You mentioned that this only affects packaged applications, so I took your fiddle, fixed the import to be require('electron-store'), and packaged it with Fiddle. When running on macOS, I get:

Image

So to me this looks like a bug.

For context: I checked and Fiddle packages apps without ASAR archives.

Looks like this boils down to one of our patches only covering the usual async case, but not the sync case that occurs in a packaged app:

https://github.com/electron/electron/blob/c2ab63f6d7091f9412362136d3ed66a563d33082/patches/node/fix_do_not_resolve_electron_entrypoints.patch#L12-L17

Notice that this patch (currently) changes only defaultLoad and the stack trace in the error of this issue includes defaultLoadSync.

Will have a fix up shortly.

I found that the “electron-is-dev” module can cause problems.

@stevennoyce I don’t believe there is any stable version that this issue is not present in, 35 was the first version of Electron to officially allow support for requiring ESM modules.

hmm I wonder could it be because electron-store is importing election? I also tried electron-is-dev with the same result. Which is the same dev.