Javascript

Uncaught ReferenceError React is not defined

27 September 2026 · 9 min read

Uncaught ReferenceError React is not defined

Encountering the dreaded Uncaught ReferenceError: React is not defined can be a frustrating roadblock when developing React applications. This error, often appearing seemingly out of nowhere, signifies that the JavaScript code attempting to use React cannot find the React library itself. It’s a common issue, especially for beginners, but understanding its root causes and implementing the correct solutions can save you valuable debugging time. We’ll delve into the common reasons behind this error, such as incorrect script loading order, missing dependencies, and problems with your build process. Master these troubleshooting steps, and you’ll be equipped to tackle this challenge head-on and get your React projects back on track. Understanding the intricacies of JavaScript environments and dependency management is crucial for avoiding this persistent problem, ensuring your application runs smoothly and efficiently.

Understanding the “React is not defined” Error

The “Uncaught ReferenceError: React is not defined” error message indicates that the JavaScript code trying to use React functions or components can’t find the React library in the current scope. This typically happens when the React library hasn’t been properly loaded or linked in your HTML file, or when there are issues with how your modules are being bundled. The browser essentially throws this error because it encounters the React object without it being previously declared or made available. Without the React library available, your application won’t be able to render React components or utilize React’s functionalities, leading to a broken user interface and application logic.

Several factors can contribute to this error. A very common reason is forgetting to include the necessary

Let’s consider a simple example. Suppose you have a basic React component called MyComponent that renders a “Hello, World!” message. If you attempt to render this component in your HTML without first including the React and ReactDOM libraries, you’ll inevitably encounter the “Uncaught ReferenceError: React is not defined” error. This highlights the importance of ensuring that all required dependencies are correctly loaded before executing any React-related code. Proper dependency management ensures that your application has access to the necessary libraries and avoids common runtime errors.

Common Causes and Troubleshooting Steps

Pinpointing the exact cause of the “Uncaught ReferenceError: React is not defined” error requires a systematic approach. Let’s explore some of the most common reasons and the steps you can take to resolve them. A primary suspect is the script loading order within your HTML file. JavaScript files are executed sequentially, so if your component’s script is loaded before the React library, the browser will throw the error. Another common problem is that the React libraries might not be included at all.

Here’s a breakdown of the most frequent issues and how to address them:

  • Missing Script Tags: Ensure that you have included the necessary
  • Incorrect Script Order: Make sure that the React and ReactDOM scripts are loaded before your component scripts. The order matters!
  • Module Bundler Issues: If you’re using Webpack, Parcel, or a similar bundler, verify that React is correctly included in your bundling configuration. Check your webpack.config.js or equivalent file.

To illustrate further, imagine you’re building a complex application with several React components. You’re using Webpack to manage your modules, but a misconfiguration in your webpack.config.js file prevents React from being correctly bundled into the final JavaScript file. This leads to the browser being unable to find the React library when it tries to render your components, resulting in the “Uncaught ReferenceError: React is not defined” error. By carefully reviewing your Webpack configuration and ensuring React is correctly included, you can resolve this issue and get your application running smoothly. Remember to clear your cache after making configuration changes. One can find more about webpack configurations at the official Webpack documentation site.

Step-by-Step Solutions to Fix the Error

Let’s walk through a detailed, step-by-step approach to fixing the “Uncaught ReferenceError: React is not defined” error. These practical steps, when followed methodically, will help you identify and resolve the underlying issue, restoring your React application to proper functionality. Each step is designed to address a specific potential cause, ensuring a thorough troubleshooting process.

  1. Verify Script Tags: Open your HTML file and carefully check for the presence of the React and ReactDOM
  2. Check Script Order: Confirm that the script tags for React and ReactDOM are placed before any script tags that load your React components. The order of execution matters, so ensure that React is loaded before any code that depends on it.
  3. Inspect Module Bundler Configuration: If you’re using a module bundler like Webpack, examine your configuration file (e.g., webpack.config.js). Verify that React is correctly included as a dependency and is being bundled into the output JavaScript file. Make sure your configuration is not excluding React unintentionally.
  4. Clear Cache: After making changes to your build configuration, clear your browser cache and any build caches used by your module bundler. Cached versions of your code can sometimes prevent changes from taking effect.
  5. Check Node Modules: If you’re using npm or yarn, ensure that React is installed as a dependency in your package.json file and that the node_modules directory contains the React library. Run npm install or yarn install to ensure all dependencies are properly installed.

For example, if you are using Create React App, which abstracts away much of the Webpack configuration, the issue might stem from accidentally deleting or modifying the pre-configured scripts. In such cases, you may need to recreate the app or carefully compare your project structure with a fresh Create React App project to identify any discrepancies. Remember to consult the official Create React App documentation for guidance.

Using CDNs vs. Local Installations

When including React in your project, you have two primary options: using Content Delivery Networks (CDNs) or installing React locally via a package manager like npm or yarn. Each approach has its own set of advantages and disadvantages. Understanding these differences can help you choose the method that best suits your project’s needs and development workflow.

CDNs offer a convenient way to include React without the need to manage local files. They provide readily available links to the React libraries, which you can directly include in your HTML file using

Local installations, on the other hand, involve installing React as a dependency in your project using npm or yarn. This approach gives you greater control over the version of React being used and allows you to work offline. Local installations are typically used in conjunction with module bundlers like Webpack or Parcel, which bundle all of your project’s dependencies into a single JavaScript file. This approach is generally preferred for larger, more complex projects, as it provides better dependency management and build optimization. However, it also requires more setup and configuration compared to using CDNs. Choosing the right approach depends on your project’s specific requirements and your familiarity with different development tools. According to Stack Overflow’s 2023 Developer Survey, a vast majority of professional developers use package managers like npm and yarn for dependency management, highlighting the prevalence of local installations in modern web development. You can review the survey at Stack Overflow Developer Survey 2023.

Here’s a quick summary:

  • CDNs: Easy to set up, potentially faster loading times, requires an internet connection, less control over versioning.
  • Local Installations: More control over versioning, allows offline development, requires more setup, better suited for larger projects.

This paragraph is optimized for featured snippets. When choosing between CDNs and local installations for React, consider your project’s size and complexity. CDNs are ideal for quick prototypes or smaller projects where ease of setup is paramount. Local installations with module bundlers provide better dependency management and control for larger, more complex applications, allowing for offline development and precise version control of the React library.

FAQ: React is not defined

Why am I getting "React is not defined" even though I have the script tag?
Double-check the order of your script tags. The React and ReactDOM scripts must be loaded before your component scripts. Also, verify that the script tags are correctly pointing to the React libraries.
How do I fix "React is not defined" when using Webpack?
Ensure that React is correctly included in your webpack.config.js file. You might need to explicitly import React in your component files if you're not using JSX.
Is it better to use a CDN or local installation for React?
It depends on your project. CDNs are easier for quick setups, while local installations offer more control and are better for larger projects.
Can a browser extension cause "React is not defined"?
In rare cases, yes. Some browser extensions can interfere with script loading. Try disabling extensions one by one to see if that resolves the issue.
Infographic here: Comparison of CDN vs. Local Installation for React
Addressing the "**Uncaught ReferenceError: React is not defined**" requires a careful review of your project setup, focusing on script loading order, dependency management, and build configurations. By systematically checking these areas and implementing the solutions outlined above, you can quickly resolve this error and ensure your React applications run smoothly.

Don’t let this error slow you down. Now that you understand the common causes and how to fix them, take the next step. Review your own React projects, apply these troubleshooting techniques, and get back to building amazing user interfaces. If you’re interested in diving deeper into React development, consider exploring more advanced topics like state management or testing. You can also check out this helpful resource for additional tips and tricks.

Question & Answer :
I am trying to make ReactJS work with rails using this tutorial. I am getting this error:


Uncaught ReferenceError: React is not defined

But I can access the React object in browser consoleenter image description here
I also added public/dist/turbo-react.min.js as described here and also added //= require components line in application.js as described in this answer to no luck. Additionally,
var React = require('react') gives the error:
Uncaught ReferenceError: require is not defined

Can anyone suggest me on how to resolve this?

[EDIT 1]
Source code for reference:
This is my comments.js.jsx file:

var Comment = React.createClass({ render: function () { return ( <div className="comment"> <h2 className="commentAuthor"> {this.props.author} </h2> {this.props.comment} </div> ); } }); var ready = function () { React.renderComponent( <Comment author="Richard" comment="This is a comment "/>, document.getElementById('comments') ); }; $(document).ready(ready); 

And this is my index.html.erb:

<div id="comments"></div> 

If you are using Babel and React 17, you might need to add “runtime”: “automatic” to Babel config.

In .babelrc config file this would be:

{ "presets": [ "@babel/preset-env", ["@babel/preset-react", {"runtime": "automatic"}] ] }