Skip to content

Conversation

@MaxBlack-dev
Copy link
Contributor

@MaxBlack-dev MaxBlack-dev commented Nov 30, 2025

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

  • Documentation update

@MaxBlack-dev MaxBlack-dev requested a review from a team as a code owner November 30, 2025 01:37
@wraithgar
Copy link
Member

wraithgar commented Dec 2, 2025

This likely needs a discussion. There is a similar PR on the docs themselves for the type field that has the context. The TLDR is that Node.js drives this field, npm does not parse it at all so the docs need to point to the source, and not try to recreate it in a way that needs maintenance on our end.

ETA: added context to the issue too #8376 (comment)

@MaxBlack-dev
Copy link
Contributor Author

@wraithgar Thank you for the feedback! I've updated the PR to follow the same approach discussed in npm/documentation#1653 - the documentation now simply references the Node.js documentation as the source of truth, avoiding technical details that would need maintenance. Please let me know if this approach works better.

@wraithgar wraithgar merged commit 50508f9 into npm:latest Dec 3, 2025
8 checks passed
@github-actions github-actions bot mentioned this pull request Dec 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[DOCS] package.json type field is undocumented

2 participants