-
Notifications
You must be signed in to change notification settings - Fork 147
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 ES module import in renderer not following esm package resolution #387
Comments
Maybe something like this in const { BrowserWindow } = require('electron')
BrowserWindow.prototype.require = require("esm")( module/*, options*/ )
require = require("esm")( module/*, options*/ )
module.exports = require( `./${require('./package.json').module}` || "./main.js" ) This is just a way to illustrate the idea because the require method does not exist and the renderer module is to be initialized for each instance instead of here... |
Hi @devingfx! The Update: For reference I've modified the |
So this could be solved with a simple preload script (when creating BrowserWindow, they have an option to pre-load a script), with a simple script as below: var _require = require("esm")(module)
process.once('loaded', () => {
global.require = _require
}) And in your <script>
require('path/to/your/renderer/script.js')
</script> Now that import '...'
export ... @jdalton BrowserWindow actually integrates node environment into its javascript environment. Hopefully next time if anyone asks related again, this solution would help. And thanks for the great work, I love Electron + ESM. Cheers. |
Thanks @Tundon it's good to know that <script type="module">
import { BrowserWindow } from 'electron'
</script> ... directly from root HTML scope and because this execution is handled by the BrowserWindow context aka chromium it's its implementation of import/export that gain precedence... |
Hey! I just had an idea: Is it posible to give the overrided Like so it can be implemented this way: pre-load script: var _require = require("esm")(module)
process.once('loaded', () => {
global.require = _require
[...document.scripts]
.filter( s=> s.type == "esm" )
.map( s=> _require.fromString(s.textContent) )
}) html pages: <script type="esm">
import { BrowserWindow } from 'electron'
</script> |
Hi @devingfx |
Nope |
Hi!
I'm using
esm
with electron. The nodejs part of loading ES module works great but I can't figure out how to make the renderer process (aka BrowserWindow) to use esm also for module resolution.I want to be able to unify the way scripts are loaded because it's a bit weird to be able to use
import
syntax in main process but still having to userequire
in the renderer process!so I want to be able to:
but because the chromium process is able to use ES module imports, it use the browser path resolution and so I have this error:
Uncaught TypeError: Failed to resolve module specifier 'electron'
Is there a way to tell the
BrowserWindow
class to use esm instead? Or a way to teach the browser where to import 'global' module names?I finally tried to import electron from the node_modules folder directly:
but now I have this error:
Failed to load module script: The server responded with a non-JavaScript MIME type of "". Strict MIME type checking is enforced for module scripts per HTML spec.
The text was updated successfully, but these errors were encountered: