Require('jquery-ui') in node-webkit produces navigator not found error

為{幸葍}努か 提交于 2020-01-04 14:31:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!