Packages in Node.js

A package in Node.js is a folder tree described by a package.json file. The package consists of the folder containing the package.json file and all subfolders until another folder with its own package.json or a node_modules folder is encountered. The package.json file defines how .js files are interpreted (as CommonJS or ES modules), specifies entry points, and controls exports and imports. Node.js supports both CommonJS and ES modules, and package authors can use fields like type, main, exports, and imports to configure behavior.

package.jsontype: "module" / "commonjs"main و exportssubpath exports / imports

~2 min read · Updated Dec 30, 2025

1. Package Definition


A package is a folder containing a package.json file. All subfolders belong to the package until another package.json or a node_modules folder is found.


2. Determining Module System


  • .mjs files are always ES modules.
  • .cjs files are always CommonJS.
  • .js files depend on the type field in package.json.
  • If no type field exists, .js files default to CommonJS.

3. Syntax Detection


If a .js file lacks a type field, Node.js inspects the code. ES module syntax (import/export, import.meta, top-level await) forces the file to be treated as an ES module.


4. Resolution and Loading


  • require(): CommonJS resolution supports folders as modules and tries extensions (.js, .json, .node).
  • import: ES module resolution requires explicit extensions and supports URLs.

5. The type Field


The type field in package.json defines how .js files are interpreted:


// package.json
{
  "type": "module"
}

6. Entry Points


  • main: Legacy field defining the default entry point.
  • exports: Modern field allowing multiple entry points and encapsulation.

7. Subpath Exports


Custom subpaths can be defined in exports:


// package.json
{
  "exports": {
    ".": "./index.js",
    "./submodule.js": "./src/submodule.js"
  }
}

8. Subpath Imports


The imports field defines private internal mappings, starting with #:


// package.json
{
  "imports": {
    "#dep": "./dep-polyfill.js"
  }
}

9. Conditional Exports


Different paths can be defined depending on conditions:


// package.json
{
  "exports": {
    "import": "./index-module.js",
    "require": "./index-require.cjs"
  }
}

10. Self-Referencing


A package can import itself using its name, provided exports is defined:


// package.json
{
  "name": "a-package",
  "exports": {
    ".": "./index.mjs"
  }
}

Conclusion


Packages in Node.js are managed through package.json, which controls module type, entry points, and exports. Proper use of type, exports, and imports ensures predictable, secure, and compatible packages across modern tools and environments.


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