JavaScript

Difference between Import and Require in JavaScript

JavaScript is a programming language that makes our website interactive by giving it the functionality of thinking and acting. When our web application development grows, we divide our code into multiple files(for debugging code easily and code reusability) which are called modules. Modules are just like JavaScript libraries, that contain a set of commands or functions that we can add to our application. To use these modules we take the help of Import and Require methods in JavaScript.

What is require() method

Require is a built-in method of Node.js that is used to read and execute/include external modules in your current working file. When we call require() method in a JavaScript file, it reads the JavaScript file that is referenced and then executes that file. After executing, it returns the object that was exported. The require() method is used both for including built-in core modules or modules that we created locally in our application.

Syntax

const myModule = require("moduleName");

The require() method is used to reference the name of the JavaScript file/module (moduleName) that we want to include in our application.

To include built-in modules like http we can simply follow the below code:

const http = require("http");

To use a local module we pass the path of the module that we want to use in our application:

var myModule = require('./myLocaModule');

What is the import() method

The import() method was first introduced with ES6 modules and is used to refer to the ES6 module which we can use in our current working file. It should be noted that the import() method cannot be used outside ES modules; for example, we cannot import a file type of .json with the import() method. The import() method takes the URL style relative path or the package name that we want to import as its parameter.

Syntax

var myModule = import("moduleName");

Difference between Import() and require() methods

The major difference between require and import method is that require method can be called anywhere in the JavaScript file and is non-lexical (wherever it is called it stays there) however, import cannot be called conditionally and is lexical (goes to the top of the current file and executed). Import() method is always executed at the top of the file.

The next big difference between require and import methods is that when we use the require method, we have to save our javascript file with the .js extension, however when we use the import method then we have to go for the .mjs extension. From this difference, we can also see that one cannot use both the import and require methods in a single node program.

The other difference between these two methods is that the import method loads the module partially hence saving memory whereas the require method doesn’t load partially.

Conclusion

JavaScript is divided into modules/files for the purpose of easy debugging and code reusability. To call these external modules, inside the application or the page we use the import and require methods. In our application including local modules or built-in core modules of JavaScript is also achieved with require and import methods.

In this post, we discussed what require and import methods are and the difference between require and import methods in Javascript.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.