Skip to content
Explain to Dev
Explain to Dev

Empowering developers with the knowledge to build, create, and innovate in the software world.

  • Home
  • About
  • Java
  • Python
  • PHP
  • .NET
  • Node.js
  • SQL
  • Privacy Policy
Explain to Dev

Empowering developers with the knowledge to build, create, and innovate in the software world.

Understanding the Difference Between require and import in Node.js

etd_admin, November 26, 2024November 26, 2024

When working with Node.js, developers often encounter two ways to include external modules or libraries in their code: require and import. Understanding the difference between require and import in Node.js is essential for writing efficient and modern JavaScript applications. This article will explain their differences, provide clear examples, and help you decide when to use each.

What Is require in Node.js?

require is the module-loading system introduced in Node.js before the adoption of ECMAScript modules (ESM). It uses CommonJS (CJS) syntax, which has been the default module format in Node.js for years.

// Load a built-in Node.js module
const fs = require('fs');

// Load an external library
const express = require('express');

// Load a local file
const myModule = require('./myModule');

// Use the module
console.log(myModule.greet());

Key Features of require

  1. Synchronous. The require function loads modules synchronously, which can block execution during loading.
  2. CommonJS-Only. It works only with CommonJS modules.
  3. Backward Compatibility. Since most older Node.js projects use require, it ensures compatibility with legacy code.

What Is import in Node.js?

import is the syntax introduced with ECMAScript modules (ESM). It is now the official JavaScript module standard and is gradually replacing CommonJS in modern Node.js development.

// Load a built-in Node.js module
import fs from 'fs';

// Load an external library
import express from 'express';

// Load a local file
import { greet } from './myModule.js';

// Use the imported function
console.log(greet());

Key Features of import

  1. Asynchronous. Module loading happens asynchronously, improving performance in some scenarios.
  2. Standardized. It follows the ECMAScript specification, making it consistent across environments like browsers and Node.js.
  3. Tree Shaking. Unused parts of a module can be excluded from the final bundle, reducing file size.
  4. File Extension Required. When importing local modules, the .js file extension is mandatory unless resolved by a build tool.

Key Differences Between require and import

Featurerequire (CommonJS)import (ESM)
Syntaxconst module = require('')import module from ''
Module SystemCommonJSECMAScript Modules
Loading BehaviorSynchronousAsynchronous
File ExtensionsNot requiredMandatory for local files
Tree Shaking SupportNoYes
Default Support in Node.jsAlways supportedFully supported since Node.js 12 (with flags earlier)

Use require When:

  1. Working with Legacy Code. Older Node.js projects often use require.
  2. Using CommonJS Modules. If a package or module doesn’t support ESM, stick with require.

Use import When:

  1. Starting a New Project. Use the modern import syntax for better compatibility with the JavaScript ecosystem.
  2. Working in a Mixed Environment. When code needs to run on both browsers and Node.js, import ensures consistency.
  3. Tree Shaking Needed. Use import if you want to reduce unused code in the final build.

Combining require and import

If your project requires both syntaxes (e.g., using an older CommonJS library in an ESM-based project), Node.js allows a hybrid approach.

// Import ESM modules
import { readFile } from 'fs/promises';

// Use require for CommonJS modules
const path = require('path');

console.log(path.join(__dirname, 'file.txt'));

How to Enable ESM in Node.js

To use import, ensure your project supports ESM. You can do this by:

  1. Renaming files to .mjs, or
  2. Adding "type": "module" in your package.json.

The difference between require and import in Node.js lies in their syntax, behavior, and intended use cases. While require is robust and reliable for legacy code, import is the future of JavaScript modules and offers many advantages for modern development. Choosing the right one depends on your project’s needs and compatibility requirements.

Node.js Node.js

Post navigation

Previous post
Next post
©2025 Explain to Dev | WordPress Theme by SuperbThemes