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

时间:2016-01-20 21:12:28

标签: javascript node.js visual-studio processing.js referenceerror

我想在nodeJS服务器中使用processingJS作为npm包,以便在MS Azure上进行部署。我正在使用VS15。我无法参考它:

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。

提前致谢。

2 个答案:

答案 0 :(得分:1)

navigator是主机环境在桌面浏览器中可用的对象。 (很像DOM) - javascript语言没有定义navigator对象,因此V8(底层引擎)不提供它,并且由于节点不是浏览器,因此它不实现{ {1}}对象。

处理只能在浏览器中使用 - 要么你需要在节点中为它提供一个shimmed环境,要么在浏览器中使用它(无论是否无头)

答案 1 :(得分:0)

对于回顾此问题的任何人,想知道如何将处理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);
相关问题