Skip to content

Commit 50508f9

Browse files
MaxBlack-devMax Black
andauthored
docs(package-json): add documentation for type field (#8793)
## Description This PR adds documentation for the `type` field in package.json, which controls whether Node.js treats .js files as ES modules or CommonJS modules. ## Changes - Added a new section documenting the `type` field - Explained the two possible values: `module` and `commonjs` - Provided examples for both module types - Added a note about .mjs and .cjs file extensions - Linked to Node.js official documentation ## Fixes Closes #8376 ## Context The `type` field is a crucial part of package.json for modern Node.js projects, especially with the widespread adoption of ES modules. As reported in issue #8376, this field was not documented in npm's package.json reference, which caused confusion for developers trying to understand how to configure their packages for ESM or CommonJS. This documentation clarifies: 1. How to enable ES modules in a package (`"type": "module"`) 2. How to explicitly use CommonJS (`"type": "commonjs"`) 3. The default behavior when `type` is omitted (CommonJS) 4. That file extensions (.mjs/.cjs) override the type field ## Type of Change - [x] Documentation update Co-authored-by: Max Black <[email protected]>
1 parent aa1dd7e commit 50508f9

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

docs/lib/content/configuring-npm/package-json.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,12 @@ For most modules, it makes the most sense to have a main script and often not mu
335335

336336
If `main` is not set, it defaults to `index.js` in the package's root folder.
337337

338+
### type
339+
340+
The `type` field defines how Node.js should interpret `.js` files in your package. This field is not used by npm.
341+
342+
See the [Node.js documentation on the type field](https://nodejs.org/api/packages.html#type) for more information.
343+
338344
### browser
339345

340346
If your module is meant to be used client-side the browser field should be used instead of the main field.

0 commit comments

Comments
 (0)