错误:ReferenceError:未定义文档

时间:2013-08-10 23:52:59

标签: javascript node.js requirejs

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

我们正在使用 OPTIMIZEJS 这是一个 require.js 插件来编译它,而在 node.js 这个名为{{3}的模块}。

运行服务器时出现问题我收到此错误。

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

1 个答案:

答案 0 :(得分:2)

&#34; ../ util / xhr&#34;中的代码写入是在浏览器(定义文档)中执行而不是在节点中执行。由于节点不是文档,因此未定义全局文档对象。

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