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 The area of coding interviews and competitive programming is filled with questions that require you to be at your best. LeetCode is definitely an essential platform where developers perfect their technical ability and problem-solving skills. On LeetCode, a huge developer can find coding tasks varying...
15 min read
A is a sophisticated tool that helps you to intercept and alter how moves are carried out on items. A component of the ECMAScript 6 (ES6) preferred, proxies offer a way of adding unique capability to conventional functions together with reading, writing, or putting...
6 min read
We will comprehend how to convert a number to a string in JavaScript in this article. There are many methods that we can utilize to convert a number to string in JavaScript which are as follows: Utilizing the toString() Method Utilizing the String() function Concatenating an Empty String Utilizing the...
3 min read
The nullish coalescing operator (??) is a simple yet powerful linguistic addition to JavaScript that makes dealing with those null and undefined values a simple matter. The nullish coalescing operator helps with configuration objects, API responses, and optional parameters, whether you're even working with it or not....
6 min read
How to Run a JavaScript Program in Notepad 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...
6 min read
In programming, camel case is a common writing style where the first word begins with a lowercase letter, and each subsequent word begins with an uppercase letter. This naming convention is particularly used in JavaScript for variables, functions, and methods. Variables like getUserInfo, fetchDataFromAPI, and userName...
8 min read
What is Framework in JavaScript? In JavaScript, frameworks are pre-written code JavaScript libraries that can provide developers with pre-built solutions for the common tasks and challenges in web development. Most of the frameworks in JavaScript often include functions, modules, and APIs that help us simplify the process...
6 min read
One of the most powerful features of JavaScript is that it can change the styling of elements in a webpage. You can maintain a dynamic design style by customizing the style of an element using javascript language based on the input or behavior of a user....
6 min read
What is Remix.JS? In JavaScript, Remix JS is a Full Stack React-based framework that helps us to render the code on the server, which tends to result in better performance and SEO as compared to using React on the client side. In simple words, Remix is built to...
6 min read
The JavaScript programming language does not directly support multidimensional arrays. A multidimensional array must be created by employing another one-dimensional array to use something that functions as a multidimensional array. Thus, multidimensional arrays are called arrays inside other arrays in JavaScript. The primary array has the...
7 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