How to Fix “ReferenceError: document is not defined” in JavaScript

The “ReferenceError: document is not defined” is a common error in JavaScript that occurs when trying to access the document object in a non-browser environment like Node.js.

This guide will explain what causes this error and how to fix it with code examples.

What Causes the “document is not defined” Error?

The document object is part of the DOM API and is only available within web browsers. When you try to access document in other environments like:

  • Node.js server-side JavaScript
  • JavaScriptCore (iOS/Mac)
  • React Native

You’ll get the “document is not defined” reference error. This happens because the document object does not exist in those environments.

For example:

// In Node.js 

let element = document.querySelector('#some-id');
// ReferenceError: document is not defined

3 Ways to Fix the “document is not defined” Error

Here are 3 ways to fix this error with code examples:

1. Check for Document Existence First

Wrap your document code in an if statement to check if document exists first:

if (typeof document !== 'undefined') {
  let element = document.querySelector('#id');

This prevents the code from running in non-browser environments.

2. Use Browser-Only APIs Like useEffect in React

If using React, wrap DOM code in useEffect to ensure it runs only after mounting:

import React, { useEffect } from 'react';

function MyComponent() {
  useEffect(() => {
    let element = document.querySelector('#some-id');
  }, []);

  return <div>My Component</div>; 

3. Use Browser-Only Components in Frameworks Like Nuxt

For example, in Nuxt.js use the <client-only> component:

    <div id="some-id"></div>

This renders the component only on the client-side.


The “ReferenceError: document is not defined” can be a frustrating error in JavaScript. It occurs when trying to access the document object in non-browser environments like Node.js or React Native.

To fix it, you need to:

  • Check for document existence before using it
  • Use browser-only APIs like React’s useEffect()
  • Wrap browser-only code in framework components like <client-only> in Nuxt

The key is to ensure any document access is wrapped in browser-only code blocks. This prevents it from running in Node.js or other JS environments without DOM access.

Debugging JavaScript errors takes some practice and learning about the execution contexts. Now you know what causes the “document is not defined” error and how to prevent it in your code.

Following best practices like abstracting environment-specific code can help avoid similar issues. With the solutions outlined in this guide, you should be able to resolve this error when it pops up.

Hopefully this gives you a better understanding of the JavaScript document object and how to properly use it across different environments. Happy coding!

FAQs About the ReferenceError: document is not defined

Why does this error happen in Node.js?

Node.js runs JavaScript on the server, not in a browser, so the DOM APIs like document don’t exist globally.

Does this error happen only in Node.js?

No, it can happen in any JavaScript environment without a DOM, like React Native or JavaScriptCore.

How can I access the DOM in Node.js?

You can’t. Node.js is server-side JavaScript without DOM access. You need to rethink your code to run DOM access only in the browser.

What if I want to reuse code across server and browser?

Use a check like if (typeof document !== 'undefined') to wrap browser-only code. Or abstract the environment-specific parts into separate files.

Scroll to Top