5

运行我的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;
    }
  };
});
4

1 回答 1

2

“../util/xhr”中的代码被编写为在浏览器(定义文档的地方)中执行,而不是在节点中执行。由于节点不是文档,因此未定义全局文档对象。

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

于 2014-04-08T03:49:16.387 回答