Referenceerror: Document Is Not Defined (In Plain JavaScript)

Node.js document is not defined

document relates to the DOM (Document Object Model) in a web browser.

Node.js, however, is not a browser environment. It is a server environment, much like PHP or Perl, and as such, you can’t access the browser’s DOM or do anything specific to browser-hosted JavaScript.

The closest you could get is using something like browserify to include Node.js modules in your client-side code.

ReferenceError: document is not defined' error

The problem is you're requireing the code that uses document in the global scope before you declare the document.

var assert = require('assert');
var jsdom = require('mocha-jsdom');

global.document = jsdom();

var test = require('../index.js');

describe('Mutliply', function() { ...

should work, or even

var assert = require('assert');
var jsdom = require('mocha-jsdom');

global.document = jsdom();

describe('Mutliply', function() {
var test = require('../index.js'); // late import
it('should equal 9 when multiply is called', function() {
assert.equal(9, test.multiply());
});
});


Related Topics



Leave a reply



Submit