require(processing-js) 抛出引用错误:找不到导航器

require(processing-js) throws Reference Error: Navigator not Found

本文关键字:错误 找不到 导航 引用 processing-js require      更新时间:2023-09-26

我想将processingJS用作nodeJS服务器中的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 产生 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(底层引擎)不提供它,并且由于node不是浏览器,它也不会实现navigator对象。

处理被设计为仅在浏览器中使用——要么你需要在node中为它提供一个填充环境,要么在浏览器中使用它(无头或不外设)。

对于任何

回顾这个问题想知道如何将 processingjs 代码预编译为 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);