0

我想在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。

在此先感謝。

回答

1

navigator是宿主環境在桌面瀏覽器中可用的對象。 (與DOM非常相似) - javascript語言沒有定義navigator對象,所以V8(底層引擎)不提供它,並且因爲節點不是瀏覽器,所以它也不實現navigator對象。

處理被設計爲在瀏覽器單獨使用 - 無論你將需要以節點爲它提供一個勻的環境,或在瀏覽器中使用它(或者無頭或沒有)

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);