How to use the require() Function in JavaScript

In this article, we will explore the require function in JavaScript.

The require function is a native function in JavaScript. It plays a crucial role within the CommonJS module system, allowing you to import modules and enabling the use of external modules within your code.

Modules bear resemblance to JavaScript libraries, yet they are more compact, consisting of segments of code. Each module typically encompasses classes and functions that contribute to enhancing the code quality and ensure a structured approach to coding. These modules can either be provided by the standard library or created by the user. Both built-in and user-defined modules can be imported for use when necessary. Additionally, there are instances where external modules need to be imported from the internet, which can be accomplished by leveraging web-based URLs.

How to use the require function?

Utilizing the require function is straightforward. All that is needed is to assign a function to a variable.

General syntax:

Example

const var_name = require(location_name);

In the syntax provided above, var_name refers to the identifier of the variable, while require is the function that takes a location name as its parameter.

Syntax for importing external modules:

Example

var my_var = require('http://location');

In the syntax provided above, my_var represents the variable's name, while require is the function that takes an external path as its argument.

Syntax for importing a local file:

Example

var my_var = require('./location');

In the syntax presented above, my_var serves as the variable identifier. The function require is utilized, taking the path to the local file as its argument. This path is represented using dot notation, which specifies the base directory of the folder.

CommonJS and ESM Module formats

For an extended period, the CommonJS module served as the standard module system within the Node.js environment; however, a new module system known as the ECMAScript module has also been adopted. Both of these module types integrate seamlessly with Node.js, yet their execution processes differ significantly from one another.

CommonJS

In Node.js, the standard module format utilizes the require function for importing modules and the module.exports object for exporting them.

Example

Let us understand it with the help of examples.

//file1.js

Example

let add = function(x, y) {
return x + y;
};
module.exports = add;

We have created a file called file1.js that contains the function add. In Node.js, module.exports is an object that specifies which functionalities we intend to expose for use.

//file2.js

Example

let add = require('./file');
console.log(add(31, 32));

We have utilized the require('./file') method to bring in the add function from file1.js. This approach allows us to avoid rewriting the entire code. Instead, we can conveniently import the function and invoke it whenever necessary.

Output:

ECMAScript Modules (ESM)

The ECMAScript module format was first introduced in ECMAScript 2015, marking it as a contemporary method for module handling. This format is currently supported by all modern web browsers. It employs keywords like import and export that facilitate the inclusion of modules within applications.

Example:

//greetings.mjs

Example

export function greetings(name) {
  return `Hi, ${name}!`;
}

//index.mjs

Example

import { greetings } from './greet.mjs';
console.log(greetings('Example'));

Output:

Output

Hi, C# Tutorial!

Note: Old browsers may not support ECMAScript module so you may require to utilized bundler such as Browserify and Webpack or any other alternative.

Conclusion:

In this article, we have explored the require function in JavaScript. This function serves as an excellent resource for structuring and managing code effectively.

Input Required

This code uses input(). Please provide values below: