ECMAScript Modules in Node.js

ECMAScript modules (ESM) are the official standard for packaging JavaScript code for reuse. They use import and export statements and are fully supported in Node.js, alongside interoperability with CommonJS. Node.js allows developers to explicitly mark files as ES modules using .mjs, the "type": "module" field in package.json, or the --input-type=module flag. ES modules provide modern features such as dynamic imports, import.meta properties, and support for JSON and WASM imports.

import / export.mjs / .cjs / package.json typerelative, bare, and absolute specifiersfile:, node:, data: URLs

~4 min read · Updated Dec 29, 2025

1. Introduction


ES modules are defined using import and export. For example:


// addTwo.mjs
export function addTwo(num) {
  return num + 2;
}

// app.mjs
import { addTwo } from './addTwo.mjs';
console.log(addTwo(4)); // Prints: 6

2. Enabling ES Modules


  • Files with .mjs extension.
  • package.json with "type": "module".
  • --input-type=module flag.

Conversely, CommonJS can be enforced with .cjs or "type": "commonjs".


3. Import Specifiers


  • Relative: ./startup.js, ../config.mjs.
  • Bare: some-package, some-package/shuffle.
  • Absolute: file:///opt/nodejs/config.js.

Relative and absolute imports require explicit file extensions.


4. URLs


  • Modules are resolved and cached as URLs.
  • file:, node:, and data: schemes are supported.
  • data: imports support JavaScript, JSON, and WASM.

5. Import Attributes


Import attributes allow specifying type information:


import fooData from './foo.json' with { type: 'json' };

The type: 'json' attribute is mandatory for JSON imports.


6. Built-in Modules


Built-in modules provide named exports and a default export:


import { readFile } from 'node:fs';
import EventEmitter from 'node:events';

Named exports can be synchronized using module.syncBuiltinESMExports().


7. Dynamic import()


import() expressions provide asynchronous loading of ES or CommonJS modules.


8. import.meta


  • import.meta.url: Absolute file URL of the module.
  • import.meta.filename: Full resolved path of the module.
  • import.meta.dirname: Directory name of the current module.
  • import.meta.main: True if the module is the entry point.

9. Example: import.meta.main


export function foo() { return 'Hello, world'; }

function main() {
  console.log(foo());
}

if (import.meta.main) main();

Conclusion


ECMAScript modules in Node.js provide a modern, standardized way to structure applications. With explicit import/export syntax, support for JSON and WASM, and features like import.meta, ES modules enable clean, interoperable, and future-proof code organization.


1. import.meta.resolve


import.meta.resolve(specifier) returns the absolute URL string for a module specifier relative to the current module. It supports Node.js resolution rules and package exports.


const asset = import.meta.resolve('component-lib/asset.css');
// file:///app/node_modules/component-lib/asset.css

import.meta.resolve('./dep.js');
// file:///app/dep.js

This feature may involve synchronous file system operations and is not available in custom loaders.


2. Interoperability with CommonJS


  • import statements can load CommonJS modules.
  • The module.exports object is provided as the default export.
  • Static analysis attempts to expose named exports for compatibility.

// cjs.cjs
exports.name = 'exported';

// ESM
import { name } from './cjs.cjs';
console.log(name); // 'exported'

3. Differences Between ESM and CommonJS


  • No require, exports, or module.exports in ESM.
  • __filename and __dirname are replaced by import.meta.filename and import.meta.dirname.
  • Addons must be loaded via module.createRequire() or process.dlopen.
  • require.main is replaced by import.meta.main.
  • ESM uses its own cache, separate from require.cache.

4. JSON Modules


JSON imports require the with { type: 'json' } attribute:


import config from './package.json' with { type: 'json' };

5. WASM Modules


  • Source Phase Imports: Import a WebAssembly.Module object for custom instantiation.
  • Instance Phase Imports: Import .wasm files as normal modules with exports.

import source libraryModule from './library.wasm';
const instance = await WebAssembly.instantiate(libraryModule, importObject);

6. Top-level await


ESM supports await at the top level:


// a.mjs
export const five = await Promise.resolve(5);

// b.mjs
import { five } from './a.mjs';
console.log(five); // 5

7. Resolution Algorithm


The ESM resolver in Node.js is URL-based and includes:


  • Relative and absolute URL resolution.
  • No default extensions.
  • No folder mains.
  • Bare specifier resolution via node_modules.

Conclusion


import.meta.resolve and ESM/CommonJS interoperability in Node.js provide powerful tools for module management. With support for JSON, WASM, and top-level await, developers can build modern, flexible, and efficient applications.


Written & researched by Dr. Shahin Siami

Related Articles

Comprehensive Guide to the Node.js VM Module (node:vm)

The node:vm module allows you to compile and execute JavaScript code inside isolated V8 contexts — essentially creating lightweight sandboxes within your Node.js application. These contexts have their own global scope and can run code independently from the main environment. However, vm is NOT a security sandbox. It is powerful for dynamic code execution, template engines, REPLs, plugin systems, and controlled module execution, but it must never be used to run untrusted code.

Continue

Comprehensive Guide to the V8 Module in Node.js (node:v8)

The node:v8 module exposes low-level APIs that interact directly with the V8 JavaScript engine embedded in Node.js. . These APIs provide access to heap statistics, heap snapshots, coverage tools, serialization mechanisms, V8 flags, object queries, and promise lifecycle hooks. The module is essential for performance analysis, memory debugging, tooling, and advanced Node.js internals work.

Continue

Comprehensive Guide to Node.js Worker Threads (node:worker_threads)

The node:worker_threads module enables true multithreading in Node.js by running JavaScript in separate threads. While Node.js is traditionally single‑threaded, worker threads allow CPU‑intensive tasks to run in parallel without blocking the event loop. They support shared memory, zero‑copy transfers, worker pools, resource limits, and advanced synchronization APIs. Worker threads are ideal for heavy computation, data processing, and parallel workloads—while async I/O remains best handled by the main thread.

Continue

Comprehensive Guide to the Node.js util Module (node:util)

The node:util module provides a powerful collection of helper functions used throughout Node.js core and extremely useful for application developers. These utilities support debugging, inspection, formatting, type checking, callback/Promise conversions, argument parsing, text encoding, MIME handling, and more. It is one of the most versatile and essential toolkits in the Node.js ecosystem.

Continue

Comprehensive Guide to the URL Module in Node.js

The node:url module provides tools for parsing, constructing, and manipulating URLs. Node.js supports two URL APIs: WHATWG URL API — modern, browser‑compatible, standards‑based. Legacy Node.js URL API — older, Node‑specific, now discouraged. The WHATWG API is the recommended approach for all modern applications. It provides a clean, consistent interface for working with URL components, query parameters, and structured URL patterns.

Continue

راهنمای جامع UDP / Datagram Sockets در Node.js

ماژول node:dgram پیاده‌سازی کامل سوکت‌های UDP را در Node.js فراهم می‌کند. UDP یک پروتکل سبک، بدون اتصال (connectionless) و مناسب برای برنامه‌های بلادرنگ مانند VoIP، بازی‌ها، IoT، سیستم‌های پخش (broadcast) و چندپخشی (multicast) است. این ماژول امکان ساخت سوکت، ارسال و دریافت دیتاگرام، مدیریت TTL، عضویت در گروه‌های multicast، کنترل بافرها، و مدیریت رفتار سطح پایین شبکه را فراهم می‌کند.

Continue