ReferenceError: require is not defined error typically occurs if the
“JavaScript require method is used but is not available in the current context.”
The
require() function
is specific to the
CommonJS
module system,
predominantly used in
Node.js.
When you attempt to use require in an environment that doesn’t support it, such as a standard web browser or another JavaScript runtime that doesn’t implement CommonJS, you’ll encounter the “ReferenceError: require is not defined” error.
Common scenarios
Web browser environment
Web browsers do not natively support the require() function from the CommonJS module system. Instead, they use the ES6 module system, which uses import and export for module management.
Non-Node.js environment
Other JavaScript environments, like those embedded in specific desktop applications or other platforms, may not support the CommonJS module system. If they do support module systems, it might be different, like ES6 modules or something proprietary.
For these environments, developers would need to ensure that the code is compatible by avoiding require() or using some intermediary tool or library that provides similar functionality.
Reproduce the error
const fs = require('fs');
Output
Uncaught ReferenceError: require is not defined
How to fix the ReferenceError: require is not defined
Web browser environment
The <script> tags: This is the traditional way to include external JavaScript files in web pages. If your dependencies are available as standalone scripts, you can include them using <script> tags.
<script src="path_to_dependency.js"></script>
Module loaders
Tools like
RequireJS
or
SystemJS
can help manage and load modules in the browser. These loaders have their syntax and methodology for defining and loading modules.
RequireJS
: It uses the AMD (Asynchronous Module Definition) format. With RequireJS, you can asynchronously load JavaScript files as needed.
require(["dependency"], function(dependency) { // Use the loaded dependency here
Bundlers: Tools like Webpack or Parcel allow you to write modular JavaScript (using require() or ES6 import/export), which they then bundle into browser-compatible scripts. This approach is prevalent in modern web development.
Non-Node.js environment
The method to load external dependencies in non-Node.js environments will vary based on the specific environment.
Some might support ES6 modules using import/export, while others might have their proprietary system.
It’s essential to refer to the documentation or guidelines for the specific environment to understand how to load and manage external dependencies correctly.
Niva Shah is a Software Engineer with over eight years of experience. She has developed a strong foundation in computer science principles and a passion for problem-solving.
I am coding in electron.js a small application, which has several windows for different HTML, which must communicate with each other. I need to code in each HTML, the following instruction, wrapped in a “script” tag:
const { ipcRenderer } = require(‘electron’)
..and I get the error: “Uncaught ReferenceError: require is not defined”.
The require() instructions inside the main program (index.js), work fine. It only fails in the given situation.
I would be very grateful for your help, since I have searched all the documentation that exists and I have not been able to solve the problem or progress with the application.
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie
Duration
Description
cookielawinfo-checkbox-analytics
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional
11 months
The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy
11 months
The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.