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 Synchronous. The require function loads modules synchronously, which can block execution during loading. CommonJS-Only. It works only with CommonJS modules. 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 Asynchronous. Module loading happens asynchronously, improving performance in some scenarios. Standardized. It follows the ECMAScript specification, making it consistent across environments like browsers and Node.js. Tree Shaking. Unused parts of a module can be excluded from the final bundle, reducing file size. 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 ModulesLoading BehaviorSynchronousAsynchronousFile ExtensionsNot requiredMandatory for local filesTree Shaking SupportNoYesDefault Support in Node.jsAlways supportedFully supported since Node.js 12 (with flags earlier) Use require When: Working with Legacy Code. Older Node.js projects often use require. Using CommonJS Modules. If a package or module doesn’t support ESM, stick with require. Use import When: Starting a New Project. Use the modern import syntax for better compatibility with the JavaScript ecosystem. Working in a Mixed Environment. When code needs to run on both browsers and Node.js, import ensures consistency. 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: Renaming files to .mjs, or 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