require method is used but is not available in the current context.
ReferenceError: require is not defined error usually occurs when using
require in the following scenarios:
- A web browser environment: Since
requireis not a standard feature of web browsers, it will not work if it is used in a script intended to be executed in a browser.
- A non-Node.js environment: Since
requirewill not be recognized.
ReferenceError: Require is Not Defined Example
ReferenceError: require is not defined thrown trying to use the
const fs = require('fs');
In the above example, the
fs module is attempted to be imported in a web browser environment by calling the
require function. However, since
require is not available in web browsers, running the above code throws a
Uncaught ReferenceError: require is not defined
How to Fix ReferenceError: Require is Not Defined
ReferenceError: require is not defined error can be fixed by using the following approaches:
- If the error occurs in a web browser environment, a different method to load external dependencies should be considered. For example,
<script>tags or a module loader like RequireJS.
- In a non-Node.js environment, external dependencies should be loaded using a method compatible with the environment.
Track, Analyze and Manage Errors With Rollbar