问题
I have installed jquery
and jquery-ui
via npm for my node-webkit project.
I also have a index.html
which is loaded by node-webkit at startup and which loads a core.js
.
This core.js
requires both jquery
and jquery-ui
. When I start the app I get a navigator is not defined
error. I tried googling it but found no solution. Has anyone an idea what causes this?
回答1:
This problem is unrelated to jquery-ui. I can reproduce it with
// index.html
<script>
require('./test.js');
</script>
// In test.js
console.log(navigator);
This is a limitation of node's require
, which only copies over the values of global, but navigator is not actually in global
. Just specifying navigator
works in the browser's context since the implicit globals are not provided by the global
, but the window
object (try window.x = 2; global.x = 3; console.log(x);
).
To fix this problem, you can simply initialize global with the required variables from window
, or fix the code in question (i.e. jQuery UI) to prepend references to navigator with window.
. This should do for jQuery UI:
global.document = window.document;
global.navigator = window.navigator;
require('jquery-ui');
来源:https://stackoverflow.com/questions/25764696/requirejquery-ui-in-node-webkit-produces-navigator-not-found-error