Skip to content

docs: added type field in the documentation of package.json #1653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions content/cli/v11/configuring-npm/package-json.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think rather than all of this content, it should be nothing more than a link to https://nodejs.org/api/packages.html#type, so that there's not two sources of truth to keep up to date.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is generally the position we have also taken in other places in the docs. "nothing more" is a little extreme, some helpful copy explaining that the source of truth is found elsewhere would be appropriate.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh sure, that works fine :-) just, nothing technical, ie that would need to be updated in the future.

This comment was marked as spam.

This comment was marked as off-topic.


```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.
Expand Down