Javascript 错误:引用错误:未定义文档
运行node.js服务器时出现编译错误 我们正在使用OPTIMIZEJS,这是一个require.js插件来编译它,在node.js中,这个模块调用了它 当我运行服务器时,我遇到了这个错误Javascript 错误:引用错误:未定义文档,javascript,node.js,requirejs,Javascript,Node.js,Requirejs,运行node.js服务器时出现编译错误 我们正在使用OPTIMIZEJS,这是一个require.js插件来编译它,在node.js中,这个模块调用了它 当我运行服务器时,我遇到了这个错误 compilation failed for /tmp/serve.js: Error: ReferenceError: document is not defined In module tree: serve modules editor/module
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)
“./util/xhr”中的代码编写为在浏览器(定义文档的地方)中执行,而不是在节点中执行。由于节点不是文档,因此未定义全局文档对象
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;
}
};
});