I\'ve got a simple package.json file and I want to add a comment. Is there a way to do this, or are there any hacks to make this work?
As this answer explains, the // key was reserved, so it can be used conventionally for comments. The problem with // comment is that it's not practical, because it can't be used multiple times. Duplicate keys are deleted on package.json automatic updates:
"//": "this comment about dependencies stays",
"dependencies": {}
"//": "this comment disappears",
"devDependencies": {}
Another problem is that // comment can't be used inside dependencies and devDependencies because it's treated as a regular dependency:
"dependencies": {
"//": "comment"
}
npm ERR! code EINVALIDPACKAGENAME
npm ERR! Invalid package name "//": name can only contain URL-friendly characters
A workaround that works in NPM, but not Yarn, is to use a non-string value:
"dependencies": {
"foo": ["unused package"],
}
A workaround that works in NPM and Yarn is a comment added as a part of semantic versioning:
"dependencies": {
"bar": "^2",
"foo": "^2 || should be removed in 1.x release"
}
Notice that if the first part before OR doesn't match, versions from a comment can be parsed, e.g. 1.x.
Packages that need to be commented, but not installed, should be moved to another key, e.g. dependencies //:
"dependencies //": {
"baz": "unused package",
}