ReferenceError: Require is not defined.

Table of Contents What is require()?What is “Uncaught ReferenceError: require is not defined”?When it occurs? How to solve “Uncaught ReferenceError: require is not defined.”METHODS To Apply:AMD (asynchronous module definition):Conclusion: What is require()? Require() is the feature …

Require is not defined.

What is require()?

Require() is the feature or function of the NodeJS,

While using NodeJS, it is a server-side JavaScript execution function and environment that combines the version-8 JavaScript engine with several libraries. require() is one such function that you can add to your environment.

Require is a function in NodeJS that would allow you to split your JavaScript files into separate files and then use the methods or variables. So, whatever you want to use from different files in the working file on which you’re currently working.

require is defined in Node.js. Browser’s console cannot run NodeJS require function.

What isUncaught ReferenceError: require is not defined”?

“Uncaught reference error: require is not defined” is a type of error that occurs in the JavaScript environment when you want to reference the require function.

The require () is the function that is available by default in NodeJS but not in the browser console because require () is not a JavaScript function.

When it occurs? 

Whenever you try to run require () function outside the  NodeJS, you got “Uncaught ReferenceError: require is not defined” that is a client-side error because require is not the function that is built into the browser to run your script.

Browser does not have to require () when you run it on the browser, it will show you the error “reference error: require is not defined.”

How to solve “Uncaught ReferenceError: require is not defined.”

To solve the “Uncaught ReferenceError: require is not defined,” we have three methods:

  • Script tag 
  • commonJS
  • AMD

But before applying any method, install NodeJS into the machine.

If it is already installed, then check its version inside NodeJS command prompt or machine command prompt by using the command below:

Node -v

One of the most important things you make sure that the file going to be used to run this function should have a “.js” extension 

 If it is still throwing an error, then you can follow the given methods: 

  1. Include in script tag: <script>.
  2. Add synchronous dependencies in NodeJS like CommonJS.
  3. You can also add AMD (asynchronous module definition) implementation.

You can follow the given methods to make some choices about your client-side JavaScript script management.

METHODS To Apply:

Script tag use to solve Uncaught ReferenceError: require is not defined

<script src = “script/require.JS” > </script>

CommonJS

It is a client-side practice that comprises most of them requires a build step before deployment.

Change the module type to commonJS in the package.JSON file.

“Type” : “commonJS”

Browserify: you can use most of the NodeJS modules in the browser but not all the modules.

Webpack: it can do anything and everything (bundles JavaScript code, CSS, etc.) It was admired by the surge of React, but it is questionable because of its difficult learning curve.

Rollup: A new competitor holds ES6 modules and includes tree-shaking abilities (remove unused code).

AMD (asynchronous module definition):

AMD implementations include RequireJS that is well-known among all the client-side JavaScript developers. AMD method is not preferable due to its asynchronous nature.

Note: in your search for making a choice on which one to choose, Bower is only for package dependencies, and it has no opinion on module definitions like CommonJS and AMD.

Conclusion:

In this article, we have discussed how you can fix the “ReferenceError: require is not defined” and it’s possible three solution methods that you can apply on NodeJS.

Suggested Read: 500 Internal server error

Leave a Comment