Node.js
ES6
JavaScript
import/export
programming tutorials

Using Node.js require vs. ES6 import/export

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Node.js has traditionally used the CommonJS module system for managing and loading different parts of applications, primarily through the require() function. With the growing adoption of ECMAScript 2015 (ES6), which introduced a more standardized module system using import and export syntax, JavaScript developers need to understand both systems, their differences, capabilities, and limitations.

Understanding CommonJS and require()

CommonJS modules are widely used in Node.js for server-side development. Each file in Node.js is treated as a separate module, and the require() function is used to import modules.

Example of CommonJS syntax:

javascript
1// Exporting a module in CommonJS
2module.exports = {
3    add: function(a, b) {
4        return a + b;
5    }
6};
7
8// Importing a module in CommonJS
9const math = require('./math');
10console.log(math.add(1, 2));

Characteristics of CommonJS:

  • Synchronous loading: Modules are loaded synchronously, which means the code blocks until the module is fully loaded. This behavior is suitable for server-side where modules are loaded from the local disk.
  • Object exports: Modules export an object which potentially can have multiple properties and methods.
  • Dynamic loading: Modules can be conditionally and dynamically loaded within the logic of the application.

Exploring ES6 Modules import/export

ES6 modules are designed to be statically analyzable, which means that the imports and exports are declared in a standard and predictable way, allowing for optimizations like tree shaking (removing unused code).

Example of ES6 syntax:

javascript
1// Exporting in ES6
2export const add = (a, b) => a + b;
3export const multiply = (a, b) => a * b;
4
5// Importing in ES6
6import { add, multiply } from './math';
7console.log(add(1, 2));

Characteristics of ES6 Modules:

  • Static structure: The structure of imports and exports of a module are determined at compile time (before the code runs), leading to potential compile-time optimizations.
  • Asynchronous and dynamic imports: ES6 modules support dynamic import() expressions that allow asynchronous module loading, helpful in applications like SPAs (Single Page Applications).
  • Fine-grained imports and exports: Supports selectively importing or exporting parts of a module.

CommonJS vs. ES6 Modules: Key Differences

FeatureCommonJSES6 Modules
InitializationSynchronousAsynchronous
Import syntaxrequire()import
Export syntaxmodule.exportsexport
Dynamic importsSupportedSupported via import()
Static analyzabilityNoYes
Tree shaking supportNoYes

Adopting ES6 Modules in Node.js

Node.js has supported ES6 modules officially since version 13.2.0, under the --experimental-modules flag, and since Node.js 14, this support is stable. To use ES6 modules in Node.js, the files must have the .mjs extension, or the package.json must include "type": "module".

Example of Node.js with ES6 Modules:

json
{
  "type": "module"
}
javascript
1// File: math.mjs
2export function add(a, b) {
3  return a + b;
4}
5
6// File: index.mjs
7import { add } from './math.mjs';
8console.log(add(5, 3));

Conclusion

The choice between CommonJS and ES6 modules often depends on the specific needs of the project, such as the environment (browser vs. server), the need for backward compatibility, and potential benefits from static analyzability and optimizations like tree shaking. As ES6 becomes more universally supported and the JavaScript community continues to adopt modern JavaScript features, the shift toward ES6 modules seems inevitable for most new projects.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.