require(processing-js)throws参考错误:未找到导航

require(processing-js)throws参考错误:未找到导航

问题描述:

我想在nodeJS服务器中使用processingJS作为npm包以便在MS Azure上部署。我正在使用VS15。我无法引用它:require(processing-js)throws参考错误:未找到导航

var pjs = require('processing-js'); 
var http = require('http'), 
fs = require('fs'); 
var port = process.env.port || 1337; 

我的第一行代码抛出

ReferenceError: navigator is not defined 

我已经做了研究使我相信导航仪是某些属性相关的浏览器,但我很难找到更多的信息。

我已经看过这些资源,但未能拿出一个解决方案:

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

https://github.com/tobie/ua-parser/issues/440

http://fredkschott.com/post/2014/06/require-and-the-module-system/

我希望能够预编译处理成javascript。

在此先感谢。

navigator是宿主环境在桌面浏览器中可用的对象。 (与DOM非常相似) - javascript语言没有定义navigator对象,所以V8(底层引擎)不提供它,并且因为节点不是浏览器,所以它也不实现navigator对象。

处理被设计为在浏览器单独使用 - 无论你将需要以节点为它提供一个匀的环境,或在浏览器中使用它(或者无头或没有)

对于任何回头看这个问题的人都想知道如何将处理js代码预编译为javascript代码,这里是我的客户端解决方案:

var sketch = document.getElementById('processing-canvas'); 
var processingCode = 'some processingJS code as a string'; 
var jsCode = Processing.compile(processingCode); // include the processingJS api as well as processingJS in the html page you call this script from 
var processingInstance = new Processing(sketch, jsCode);