Node Require vs ES6 Import: Which One Should You Use?

We all know how important it is to import modules in Node.js to build large-scale applications. This is done to use code written by someone (third-party modules, npm etc) or using our own created modules to reduce redundancy in the code.

If you are already coding in Node.js, you know about the require() method to import modules, but is it still being used in 2024 or you should use the new ES6 import syntax? Let’s discover in this interesting tutorial and I am sure you will gain some additional knowledge about Node.js code concepts that you don’t already know.

Understanding Node.js Module Types

Module or dependency in Node.js is generally a collection of JavaScript files and folders containing related files defined by a package.json that provides some functionalities to other Node.js applications by exporting some functions, exposing APIs, and allowing code reusability. 

Modules in Node.js can be grouped into three types:

  • Core Modules: These are built-in modules that come with Node.js itself. Examples of core modules are buffer, http, fs, etc.
  • Local Modules: These types of modules are locally created by the programmer for specific use in their program.
  • Third-Party Modules: These modules are made by other programmers and are available to use and install through package managers like npm and yarn. Examples of third-party modules are express, loadash, etc.

We can create these modules in two ways:

  • CommonJS Modules
  • ECMAScript Modules

The CommonJS and ECMAScript modules influence which import statement should we use inside our script. 

Node.js Require Statement

Node.js require statement is the easiest way to import any modules in our application. It is mainly used with the CommonJS module, the default module system in Node.js.

CommonJS is the standard way to package JavaScript code for Node.js or server-side apps. To use the CommonJS syntax, our package.json file may or may not contain a “type” key at the top level.

Syntax:

The required function takes in an argument which can be a module name as a string (if it is a built-in or third-party module) or a path of the module (if it is a local module) you want to import.

// Importing the entire module
const name = require('module_name');

// Importing a local module located at './path/to/module'
const name = require('./path/to/module');

When the require statement is called, Node.js follows the following sequence of steps to load the module:

  1. Node.js tries to get the absolute path of the module by first looking into the core modules, and then inside node_modules. If an absolute path is already provided, Node.js tries to resolve that path instead of looking into the core modules or the node_modules folder.
  2. Node.js loads the file and wraps the module code in a special function that will give us access to some objects.
  3. Node.js executes the module code wrapped in the function and provides us with the exported functions, objects, and variables.
  4. Node.js even caches the module code for further use in the application.

Example:

const Utils = require('app.js');
const react = require('react');
const useState = react.useState;
const useEffect = react.useEffect;

Here the variable name used to import a module can be different from the actual module name. It is just any name used to reference the imported functionalities.

Node.js Import Statement

Node.js import is used for importing an ECMAScript or ES module. ECMAScript is the standard way to package JavaScript code for use in Browsers and other JavaScript runtimes. Node.js doesn’t use ES syntax directly but it can be used if the file extension for the module is ‘.mjs’ or the module’s nearest parent folder has { “type”: “module” } in its package.json file. Unlike the require function, the import function uses an asynchronous method to load the module. 

Syntax:

// Importing the entire module
import * as name from '<module_name>'

// Importing the default export from the module
import name from '<module_name>'

// Importing multiple exports from the module
import { name1, name2 } from '<module_name>'

// Importing a local module located at './path/to/module'
import * as name from './path/to/module'

Import allows you to selectively import different exports by using object restructuring.

Example:

import * as Utils from 'app.js'
import react from 'react'
import { useState, useEffect } from 'react'

Difference Between Require and Import

Let’s now jump into the ultimate battle of require vs import in Node.js.

requireimport
It is used with the CommonJS module systemIt is used in the ES6 module system
Synchronous loading of modules (i.e modules are loaded sequentially)Asynchronous loading of modules (i.e Node.js does not wait for the previous module to complete import)
Less efficient due to synchronous loading of modulesMore efficient and performant due to the asynchronous loading of modules
Memory usage is more as the entire module is importedObject destructuring can be used to selectively import pieces of code or named exports
It imports the components exported using module.exports keyword in the moduleImports the components exported using the export keyword in the module
Require is the default way of importing as CommonJS is the default module systemES6 or ECMAScript module system needs to be enabled to use the import statement
Require can be called anywhere in the programImport needs to be called at the top of the program
Used generally with server-side node applicationsUsed generally with Browser or front-end focused application

Choosing Between Require and Import in Node.js

Which one is best is quite difficult to say but here are some things you can consider while choosing one.

If you are new to Node.js, you should use the require statement as it is easy to use, easy to understand and is the default way to import modules, so no additional configuration is required, but there are also some limitations like it does not support module restructuring (destructuring), it is synchronous, etc. On the other hand, if you are building a large-scale application and you are importing a lot of modules into your application then import statement can be a more efficient way.

Summary

In this article, we have discussed how to import core, local, and third-party modules. The require is used in the CommonJS module system and the import in Node is introduced in ECMAScript modules (ESM) is a more standardized and modern approach. With all these, the types of modules in Node.js are also explained. If you want to know more about the Node modules, check out the articles below.

Read More:

References

Devdeep Ghosh
Devdeep Ghosh
Articles: 14