Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • NodeJS Tutorial
  • NodeJS Exercises
  • NodeJS Assert
  • NodeJS Buffer
  • NodeJS Console
  • NodeJS Crypto
  • NodeJS DNS
  • NodeJS File System
  • NodeJS Globals
  • NodeJS HTTP
  • NodeJS HTTP2
  • NodeJS OS
  • NodeJS Path
  • NodeJS Process
  • NodeJS Query String
  • NodeJS Stream
  • NodeJS String Decoder
  • NodeJS Timers
  • NodeJS URL
  • NodeJS Interview Questions
  • NodeJS Questions
  • Web Technology
Open In App
Next Article:
What is the difference between export.create and router.post?
Next article icon

Difference Between Node Require and ES6 Import And Export

Last Updated : 18 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Importing modules makes your code reusable. NodeJS offers two ways to do this: CommonJS (require()) and ES6 modules (import/export). Both achieve the same goal—sharing code, but use different syntax.

CommonJS is the older, traditional way. ES6 modules are newer and offer some advantages, but both are commonly used in NodeJS development.

Node Require v/s ES6 Import And Export

Here’s a comparison between NodeJS’s require and ES6’s import/export syntax:

Feature require (CommonJS) import/export (ES6 Modules)
Module System CommonJS ES6 Modules
Syntax const module = require('module'); import module from 'module';
Loading Synchronous Asynchronous
Dynamic Loading Supported Not natively supported (can use import() for dynamic imports)
Exports Single object (module.exports) Named exports and default exports
Usage Context Primarily used in NodeJS Supported in modern JavaScript environments, including browsers and NodeJS
File Extension .js .js or .mjs (depending on environment and configuration)
Hoisting Non-lexical (stays where placed) Lexical (hoisted to the top)
Support Widely supported in NodeJS Requires enabling ES6 modules in NodeJS (e.g., setting “type”: “module” in package.json)

In Node.js, require() and ES6 import/export each have their own use cases, so choose the one that best suits your requirements.

Node’s require (CommonJS)

The require function is part of the CommonJS module system, which NodeJS uses to handle modules. It allows you to include external modules, JSON files, or local files in your application.

// Importing a built-in module const fs = require('fs');  // Importing a local module const myModule = require('./myModule');  // Exporting a function module.exports = function() {     console.log('Hello from myModule!'); };
  • require(‘fs’): Imports the built-in file system module.
  • require(‘./myModule’): Imports a local module named myModule.
  • module.exports: Exports a function from the current module.

Key Features of require()

  • Synchronous: Modules are loaded synchronously, which means the execution waits for the required module to be fully loaded before continuing.
  • Single Export Object: A module can export multiple properties, but they are grouped into a single object.
  • Dynamic Loading: require() can be used anywhere in the code, meaning modules can be conditionally loaded.
  • CommonJS Standard: It’s specific to NodeJS and not native to browsers.

Why Use NodeJS require()?

While ES6 modules are gaining popularity, require() (CommonJS) is still widely used in NodeJS, especially in older projects and libraries. It’s important to understand how it works.

  • It’s the traditional and default way to import modules in many NodeJS projects.
  • It’s simple and has a straightforward syntax.
  • Many existing npm packages and code examples use require().

ES6 Import & Export

ES6 introduced a standardized module system with import and export statements, offering a more declarative and flexible approach to module handling. This system supports both static and dynamic imports.

// Importing a built-in module import fs from 'fs';  // Importing a local module import { myFunction } from './myModule';  // Exporting a function export function myFunction() {     console.log('Hello from myFunction!'); }
  • import fs from ‘fs’: Imports the default export from the built-in file system module.
  • import { myFunction } from ‘./myModule’: Imports a named export myFunction from a local module.
  • export function myFunction(): Exports a function named myFunction from the current module.

Key Features of import/export

  • Static Imports: Modules are loaded at the top of the file, making them statically analyzable, and cannot be conditionally loaded (though dynamic imports can be done with import()).
  • Named and Default Exports: A module can export multiple values, and they can be imported individually by name.
  • Asynchronous: ES6 modules are asynchronous by default, meaning they are loaded non-blockingly.
  • Native to JavaScript: This module system is a standard in modern JavaScript and works in both NodeJS and browser environments.

Why Use ES6 import and export?

ES6 modules (import/export) offer a modern and often preferred way to handle modules in JavaScript, including NodeJS. They bring several advantages.

  • They offer a cleaner and more declarative syntax compared to require().
  • They support asynchronous module loading, potentially improving performance.  
  • They enable static analysis, which can help catch errors during development.  
  • They are the standard for browser-based JavaScript modules, promoting consistency across environments.

Conclusion

Both require and import/export are used to load modules in NodeJS, but they differ in their module systems, loading mechanisms, and syntax. While require is synchronous and based on CommonJS, ES6 import/export offers asynchronous, statically-analyzable imports, making it more suitable for modern JavaScript development.



Next Article
What is the difference between export.create and router.post?
author
aditya_taparia
Improve
Article Tags :
  • JavaScript
  • Node.js
  • Web Technologies
  • Algorithms-Graph Traversals
  • ES6
  • Huawei
  • NodeJS-Modules
  • Scala-Arrays
  • TCS-coding-questions
  • Volkswagen IT Services
Practice Tags :
  • Huawei

Similar Reads

  • Difference Between Node.js and Asp.net
    ASP.NET: It is an open-source web application framework initially discharged by Microsoft in January 2002 with the primary cycle of the .NET system. It is built on the Common Dialect Runtime (CLR), permitting the utilization of any .NET dialect counting C# (object-oriented), F# (utilitarian to begin
    4 min read
  • Difference between React.js and Node.js
    1. React.js : This is the Javascript library that was developed by Facebook in 2013. This library was developed in order to improve and enhance the UI for the web apps but with time it has evolved a lot. It is open-source and supports different inbuilt functions and modules like form module, routing
    2 min read
  • Difference between Node.js and React.js
    Node.js and React.js are two powerful technologies widely used in the development of modern web applications. While both are based on JavaScript, they serve entirely different purposes and are used at different stages of the web development process. This article provides a detailed comparison betwee
    5 min read
  • What is the difference between export.create and router.post?
    export.create and router.post are used in the context of backend frameworks like Express JS. They both are used for handling HTTP requests and operate at different levels within the framework. In this article we will learn about export.create and router.post and see the key differences between them.
    3 min read
  • Difference between Node.js and JavaScript
    JavaScript and Node.js are both crucial in modern web development, but they serve different purposes and are used in different environments. JavaScript is a programming language primarily used for client-side web development, while Node is a runtime environment that allows JavaScript to be executed
    3 min read
  • Difference between Node.js AJAX and jQuery
    In web development, Node.js, AJAX, and jQuery play pivotal roles in creating dynamic and interactive web applications. Each technology serves a distinct purpose and is often used together to build modern web solutions. Node.js is a server-side runtime environment for JavaScript, AJAX is a technique
    4 min read
  • What are the differences between HTTP module and Express.js module ?
    HTTP and Express both are used in NodeJS for development. In this article, we'll go through HTTP and express modules separately HTTP: It is an in-build module which is pre-installed along with NodeJS. It is used to create server and set up connections. Using this connection, data sending and receivi
    3 min read
  • Difference between Node.js and Ruby on Rails
    Before stepping into a new project, the software developing team goes through a severe discussion in order to choose the best language, framework, or methodology for their project. As we are aware, the different technologies have their different pros and cons and similarly the technology which is lo
    8 min read
  • Difference Between Node.js and Python
    Node.js and Python are two of the most popular programming languages for backend development. Each has its own strengths and weaknesses, and the choice between them often depends on the specific requirements of the project. This article provides a detailed comparison of Node.js and Python, highlight
    4 min read
  • What Are The Differences Between npm and npx?
    When working with Node.js, you will frequently encounter two important tools: npm and npx. While both npm and npx are integral to managing JavaScript dependencies, they serve different purposes. The key difference between npm and npx is that npm is a package manager for installing and managing depen
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences