-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Using Client Side Library with Require #1188
Comments
This is also true for When a file is loaded via require in front-end code it is executed in a node frame, which has a different context than a browser frame. There is a I don't think this is easily solvable inside node-webkit itself. If you have access to the source code of the client side library, you should qualify all references (i.e. If you want to write your front-end code "the node way" (i.e. using require, module and exports a la CommonJS), you could also simply use browserify instead, which bundles all your front-end code into a single file you can include with a script tag. The bundle contains a require shim, which should override node-webkit's require (so you can't do crazy things like access the file system in front-end code using fs or launch an http server inside your front-end code anymore), but because it effectively loads all your front-end code from a script tag, you can use libraries that assume the global context to be the window object without problems. The obvious benefit of using browserify is that your could is easier to port to a traditional web app (i.e. node server with front-end code run in a regular browser). The obvious drawback is that the "crazy things" direct access to node's core libraries in your front-end code enables you to do are part of what makes node-webkit so uniquely interesting (making it easier to write desktop apps). |
You'd have to global it in or pass the window parameter programatically. |
@jtenner You could wrap said code in a function...e.g.: module.exports = function(window) {
// library here...
} requiring like so: library = require('library')(window) |
Thank you very much! This help was greatly appreciated. |
btw, it seems like another fix is to expose document as a global first thing: /**
* A fix for client-side components to work via require.
*/
global.document = window.document; This is working for me so far :) |
Libraries that contain references to
document
fail when usingrequire('ClientSideDOMLibraryHere')
The workaround is easy, because the library can be included as a script tag inside the document, but I was curious if this could be changed so that require has access to the document/window object of the calling window?
I'm willing to admit that I may be barking up the wrong tree here. It would be "nice" to have this feature.
The text was updated successfully, but these errors were encountered: