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
Respect browser field in package.json? #1574
Comments
There are some significant differences between node's require and things like node-browserify, modules targeted for browser might not work well in Electron. And Electron uses io.js for all node stuff, having different behaviors when loading modules doesn't sound like a good idea to me, so I'm closing this as won't fix. |
You're generally right though, while it's fine to use Electron's |
Yeah, that was my reasoning. As it stands, most isomorphic libraries will need to be updated to do browser feature detection in their node implementations. |
This little gotcha has cost me a good chunk of my time in the past week. debug
nets
I'm sure there are many more modules affected by this both directly and indirectly. Would you consider respecting the |
+1 please reopen/consider |
To be honest, these libraries need to just special-case Electron, a |
@paulcbetts could you explain a little more what those libraries would get wrong? I'm still not clearly seeing why the browser field wouldn't work. Also: how would libraries be able to special case themselves to resolve correctly in electron? I'm down to write PRs for those libraries specifically if I know how to get them to behave. Thanks! |
@yoshuawuyts So for example, many libraries which would be running under 'browser' would (quite reasonably) assume that they are running under node-browserify - in Electron, It isn't a class of issues, it's a million one-off bugs that won't be resolved by respecting |
Ah, alright. That makes sense. Thanks! |
@paulcbetts What about embedded apps, that are using |
Maybe we should agree on a node package.json |
How about just an option to respect browser field? Or a function to let the user handle it and give them full control on a per-module basis: var resolve = require('browser-resolve')
new BrowserWindow({
resolve: resolve
}) There are many instances where the "browser" field is a better choice for an Electron app. Currently the only way to get around this is to use Browserify or Webpack, which adds a lot of complexity to the development/build process. |
Having it as an option would probably be the proper resolution. I've tried working around this, and it's been painful. |
Here is how I've added this feature to devtool. The hook needs to be set before you require your entry. Once it goes through some more testing I may release it as a standalone module. |
@mattdesl that seems like a proper solution; that way electron can keep its defaults like it wants, and users can opt into different behavior if they like 👌 |
@paulcbetts is haunting me… debug-js/debug#206 (comment) See also very relevant and interesting workarounds electron/electron#1574
I've got a lil pattern for hybrid modules that need to run in Electron: var isElectron = require('is-electron')
var browser = require('./browser')
if (typeof window !== 'undefined' && isElectron()) {
module.exports = browser
} else {
module.exports = myServerFunction
} This makes it so the renderer process and browsers resolve to |
I'm a library maintainer with a package that supports both node and the browser, using a browser field in
package.json
. Recently, an issue was raised about the library not working correctly in Electron because, even though the code is being run by Chromium (the client), Electron doesn't respect the browser field when requiring modules.I realize that this isn't exactly a normal browser, but it seems to me that it would be logical for the client to use browser versions of modules when they exist. Thoughts?
The text was updated successfully, but these errors were encountered: