documentation icon indicating copy to clipboard operation
documentation copied to clipboard

doc: clarify required-ness of "name" and "version" in package.json files

Open eemeli opened this issue 3 years ago • 4 comments

The current content of the Creating a package.json file page indicates that the "name" and "version" fields are always required

A package.json file must contain "name" and "version" fields.

whereas the actual package.json docs page clarifies this with:

If you don't plan to publish your package, the name and version fields are optional.

This minor difference is causing some friction with e.g. Webpack making an assumption based on the former page, resulting in webpack/webpack#13457.

It would be good to get this specified; this PR attempts to do so with (hopefully!) sufficiently simple language.

eemeli avatar Jun 09 '22 22:06 eemeli

I think npm itself actually always requires these fields, whether the package is published or not.

ljharb avatar Jun 10 '22 04:06 ljharb

@ljharb npm requires them in the package's top-level package.json, but does not require them for package.json files that are in its subdirectories. The usage pattern that triggers this is the one documented here: https://nodejs.org/api/packages.html#type

This is useful in particular when a package transpiles TS into JS and provides both CJS and ESM endpoints: https://github.com/microsoft/TypeScript/issues/18442#issuecomment-1037056146.

eemeli avatar Jun 10 '22 12:06 eemeli

I agree, but the package being published or not isn't the discriminator - it's required in any package, including a private:true one.

ljharb avatar Jun 10 '22 13:06 ljharb

I'm not completely sure about "name", but "version" is certainly optional when a package.json includes "private": true. I use that often in e.g. documentation, when including such as npm workspaces in a monorepo.

eemeli avatar Jun 11 '22 18:06 eemeli