From 07e46eeb9d1c291d0654b526cb27d452e0ed32d5 Mon Sep 17 00:00:00 2001 From: anujupadhyay123 Date: Tue, 1 Jul 2025 12:35:07 +0530 Subject: [PATCH] docs: added type field in the documentation of package.json --- .../cli/v11/configuring-npm/package-json.mdx | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/content/cli/v11/configuring-npm/package-json.mdx b/content/cli/v11/configuring-npm/package-json.mdx index 11810e0c808..0c39637a1ee 100644 --- a/content/cli/v11/configuring-npm/package-json.mdx +++ b/content/cli/v11/configuring-npm/package-json.mdx @@ -305,6 +305,39 @@ These can not be included. The "exports" provides a modern alternative to "main" allowing multiple entry points to be defined, conditional entry resolution support between environments, and preventing any other entry points besides those defined in "exports". This encapsulation allows module authors to clearly define the public interface for their package. For more details see the [node.js documentation on package entry points](https://nodejs.org/api/packages.html#package-entry-points) +### type + +The `type` field defines the module format that Node.js should use for `.js` files in the package. When set to `"module"`, Node.js will treat `.js` files as ES modules. When set to `"commonjs"` (or when the field is omitted), Node.js will treat `.js` files as CommonJS modules. + +```json +{ + "type": "module" +} +``` + +```json +{ + "type": "commonjs" +} +``` + +When `"type": "module"` is set: +- `.js` files are parsed as ES modules +- `.mjs` files are always parsed as ES modules +- `.cjs` files are always parsed as CommonJS modules +- You can use `import` and `export` statements in `.js` files +- Top-level `await` is supported + +When `"type": "commonjs"` is set (or omitted): +- `.js` files are parsed as CommonJS modules +- `.mjs` files are always parsed as ES modules +- `.cjs` files are always parsed as CommonJS modules +- You use `require()` and `module.exports` in `.js` files + +This field applies to the entire package and all its subdirectories, unless overridden by a `package.json` file in a subdirectory. + +For more details, see the [Node.js documentation on package.json type field](https://nodejs.org/api/packages.html#type). + ### main The main field is a module ID that is the primary entry point to your program. That is, if your package is named `foo`, and a user installs it, and then does `require("foo")`, then your main module's exports object will be returned.