Since most developers are familiar with tag/annotation-based documentation, the convention I have been using is similar. I like how the @
symbol stands out from the normal declarations.
Here is a taste:
{
"@comment dependencies": {
"@comment": [
"These are the general/global comments for the `dependencies` section.",
"They are not specific to a particular package.",
"See below for how to add comments specific to a package.",
],
"@package express": [
"Use `@package {package-name}` to make comments specific to a package.",
"",
"This is a good place to explain why the version range isn't ",
"`^version` or why the latest major version isn't being used, ",
"e.g., ESM is not supported in the project at the moment."
],
"@package lodash": [
"Until we add a tool to tree shake unused parts of a lodash, ",
"only depend on the [per method packages](https://www.npmjs.com/search?q=keywords:lodash-modularized) ",
"such as `lodash.debounce`."
]
},
"dependencies": {
...
},
"@comment scripts": {
"@comment Use with NPM": [
"Be sure to add ` -- ` before adding options.",
"For example, `npm run build -- --opt1 val1`."
],
"@comment Use with Yarn": "...",
"@script build": "This comment is about the build script.",
"@script start": [
"This comment is about the `start` script.",
"It is wrapped in an array to allow line formatting.",
"",
"@option {number} --port - The port the server should listen on."
],
"@script test": "This comment is about the test script.",
},
"scripts": {
"build": "...",
"start": "...",
"test": "..."
}
}
Note: The name of the section being commented is included in the key after the @comment {section-name}
to ensure the keys are unique. That is, using just "@comment" would not be sufficient to keep keys unique if you need to add another comment at the same level. If the keys weren't unique, JSON validators, such as ones built into IDEs, will complain, or some tools, such as running npm install something
, will rewrite the package.json
file but with duplicate keys removed.
Because JSON doesn't allow a multi-line string or understand a line continuation operator/character, just use an array for each line of the comment.
It is recommended to use Markdown or something similar to help clarify your comments.
Note: For the dependencies
, devDependencies
, etc. sections, the comment annotations can't be added directly above the individual package dependencies inside the configuration object since npm
is expecting the key to be the name of an npm package. Hence the reason for the @comment dependencies
.
Note: For the dependencies
, devDependencies
, scripts
, etc. sections, the embedded @comment
keys may or may not contain a section name.
Older dependencies
Recommendation
The following was my previous recommendation for the various dependency sections (dependencies
, devDependencies
, etc).
{
"@comment dependencies": [
"These are the comments for the `dependencies` section.",
"The name of the section being commented is included in the key after the `@comment` 'annotation'/'tag' to ensure the keys are unique.",
"That is, using just \"@comment\" would not be sufficient to keep keys unique if you need to add another comment at the same level.",
"Because JSON doesn't allow a multi-line string or understand a line continuation operator/character, just use an array for each line of the comment.",
"Since this is embedded in JSON, the keys should be unique.",
"Otherwise JSON validators, such as ones built into IDEs, will complain.",
"Or some tools, such as running `npm install something --save`, will rewrite the `package.json` file but with duplicate keys removed.",
"",
"@package react - Using an `@package` 'annotation` could be how you add comments specific to particular packages."
],
"dependencies": {
...
},
"scripts": {
...
},
Older scripts
Recommendation
The following was my previous recommendation. It in-lined comments for the scripts, but I've come to realize that those comments show up as "commands" in some tools (in VS Code > Explorer > NPM Scripts section). The latest recommendation does not suffer from this issue but the script comments are no longer co-located.
{
"@comment dependencies": [
...
],
"dependencies": {
...
},
"scripts": {
"@comment build": "This comment is about the build script.",
"build": "...",
"@comment start": [
"This comment is about the `start` script.",
"It is wrapped in an array to allow line formatting.",
"When using npm, as opposed to yarn, to run the script, be sure to add ` -- ` before adding the options.",
"",
"@option {number} --port - The port the server should listen on."
],
"start": "...",
"@comment test": "This comment is about the test script.",
"test": "..."
}
}
Note: In certain contexts, such as in the "scripts"
object, some editors/IDEs may complain about the array. In the scripts context, VS Code expects a string for the value — not an array.
package.json
files and there is apackage.json
specific answer on the NodeJS mailing list. – Agnespackage.json
. Please comment on that issue - maybe we can show how useful comments can be. – Ayah