How to use the require() Function in JavaScript?15 Feb 2025 | 3 min read In this article, we will understand the require() function in JavaScript. The require() function is a built-in function in JavaScript. It is an essential part of CommonJS module system which is used to import modules and lets you utilize external modules in your code. Modules are similar to JavaScript library but modules are small consisting of a piece of code. It contains classes and function which aids in improving the code and helps in keeping the code in an organized manner. These modules can be built-in or customized. We can import both built-in and customized modules and use it when required. We sometimes need to import external module from the web and we can do that by utilizing the web-based location. How to use the require() function?It is simple to use the require() function. We have to simply assign a function to a variable. General syntax: In the above syntax, var_name is the name of the variable, require() is the function containing location name as the argument. Syntax for importing external modules: In the above syntax, my_var is the name of the variable, require() is the function containing external location as the argument. Syntax for importing a local file: In the above syntax, my_var is the name of the variable, require() is the function containing the location of local file as the argument which is denoted by dot notation which defines the root location of the folder. CommonJS and ESM Module formatsThe CommonJS module was the default module in the Node.js ecosystem for a long time but a new module called ECMAScript module is also utilized. Both of these modules work with Node.js without any problem but their execution is different from each other. CommonJSIt is the default module format in Node.js which imports modules with the help of the require() function and the module.exports object. Example Let us understand it with the help of examples. //file1.js We have made a file named file1.js which consists of function add. The module.exports is an object in Node.js which defines what tools we want to allow. //file2.js We have used a require('./file') function which imports add function from the file1.js. Now, we do not need to write the whole code again. We have simply imported the function and used it when required. Output: 63 ECMAScript Modules (ESM)The ECMAScript module was introduced in ECMAScript 2015 and it is a modern module format. It is now supported in modern browsers. It utilizes keywords such as import and export which is used to import modules. Example: //greetings.mjs //index.mjs Output: Hi, Javatpoint! 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:We have understood the require() function in JavaScript in this article. This function is an amazing tool for organizing and maintaining code properly. Next TopicImplicit-type-coercion-in-javascript |
Introduction to MapReduce in JavaScript The MapReduce process, as applied to JavaScript, is to break down a large task into smaller and manageable pieces using a simple JavaScript code. In the modern digital environment, we are coming across ever-growing amounts of data that entrepreneurs and organizations are generating...
15 min read
JavaScript ignoreCase property helps to return the Boolean value to see the availability of the "i" modifier. If the "i" modifier sets, the output shows a true value; otherwise, it shows false. The ignore-case property offers the operations of the data without a case-sensitive format. Syntax The...
2 min read
JavaScript is a flexible programming language that is mainly used for online development, but it can also be used with simple programs like Notepad. Even though advanced Integrated Development Environments (IDEs) are readily available, learning the foundations of coding in a simple setting promotes a...
6 min read
The regex "\0" shows the available null character in the input string in JavaScript. If the null value is available, then the position of the values shows; otherwise, the "-1" value shows using the search method. We can test, search, and match the required string...
8 min read
JavaScript is one of the flexible, powerful programming languages that software developers and web designers use. It has thousands of applications and can be utilized in developing various web applications and websites, even mobile applications. This article will explore the methods for adding multiple classes using...
6 min read
JavaScript, often abbreviated as JS, is a versatile programming language that powers much of the dynamic content and interactivity on the web. One fundamental aspect of JavaScript programming involves working with key-value pairs, which are at the heart of many data structures and operations within the...
9 min read
FizzBuzz is a classic coding problem often used in technical interviews to assess a candidate's basic programming skills. Despite its simplicity, it's a good test of one's ability to understand conditional logic and loop structures. In this article, we'll explore how to implement , discussing...
10 min read
Introduction: JavaScript, one of the most broadly involved programming dialects for web improvement, offers a flexible arrangement of administrators that engage engineers to control data productively. Among these administrators, the XOR (restrictive or) administrator stands apart as an integral asset for performing bitwise operations and carrying...
3 min read
JavaScript serves as the pillar of modern web development, endowing websites and online applications with dynamic and interactive content that is seen on nearly all websites today. Learning the Java script is a vital requirement for anybody who dreams of working in the web development...
6 min read
In this article, we will understand the FizzBuzz program in JavaScript. Let us first understand what "FizzBuzz" is. FizzBuzz FizzBuzz, in general, is a word game for children through which they learn to divide numbers. In this game, participants sit in a circle and then count from...
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India