错误:ReferenceError:文档未定义

运行我的node.js服务器时出现编译错误。

我们使用OPTIMIZEJS这是一个require.js插件来编译它,在node.js中这个模块叫做requirejs-middleware

当我运行服务器的问题我得到这个错误。

compilation failed for /tmp/serve.js: Error: ReferenceError: document is not defined In module tree: serve modules editor/module editor/editor editor/trackevent l10n core/localized at eval (eval at <anonymous> (/Users/alihuta2002/work/servejs/node_modules/requirejs-middleware/node_modules/requirejs/bin/r.js:22404:38), <anonymous>:6:21) 

所以我怀疑这个问题可能是文件的用法?

 define( [ "../util/xhr" ], function( xhr ) { var _strings, _readyCallback, _isReady = false; function ready( json ) { _readyCallback = _readyCallback || function(){}; function domReady() { // If the DOM isn't ready yet, repeat when it is if ( document.readyState !== "complete" ) { document.onreadystatechange = domReady; return; } document.onreadystatechange = null; _strings = json; _isReady = true; _readyCallback(); } domReady(); } // Get the current lang from the document's HTML element, which the // server set when the page was first rendered. This saves us having // to pass extra locale info around on the URL. function getCurrentLang() { var html = document.querySelector( "html" ); return html && html.lang ? html.lang : "en-US"; } xhr.get( "/strings/" + getCurrentLang(), function( res ) { ready( res ); }); return { get: function( key ) { if ( !_strings ) { console.error( "[popcorn.webmaker.org] Error: string catalog not found." ); return ""; } return ( _strings[ key ] || "" ); }, getCurrentLang: getCurrentLang, // Localized strings are ready ready: function( cb ) { _readyCallback = cb; if ( _isReady ) { _readyCallback(); } }, isReady: function() { return !!_isReady; } }; }); 

“../util/xhr”中的代码被写入在浏览器(其中定义了文档)中而不是在节点中执行。 由于节点不是文档,全局文档对象没有被定义。

https://developer.mozilla.org/en-US/docs/Web/API/document