Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: ReferenceError: document is not defined

I'm getting a compile error when running my node.js server.

We are using OPTIMIZEJS which is a require.js plugin to compile it and in node.js this modules called requirejs-middleware.

The problem when I ran the server I'm getting this error.

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)

So I suspect that the problem could be this file with the usage of document??

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;
    }
  };
});
like image 635
Ali Avatar asked Nov 01 '22 16:11

Ali


1 Answers

The code in "../util/xhr" is written to be executed in a browser (where document is defined) and not in node. Since node is not a document the global document object is not defined.

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

like image 75
user1934853 Avatar answered Nov 13 '22 20:11

user1934853