doc: clarify required-ness of "name" and "version" in package.json files
The current content of the Creating a package.json file page indicates that the "name" and "version" fields are always required
A
package.jsonfile 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.
I think npm itself actually always requires these fields, whether the package is published or not.
@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.
I agree, but the package being published or not isn't the discriminator - it's required in any package, including a private:true one.
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.