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?
DISCLAIMER: you probably should not use this hack. See comments below.
Here is another hack for adding comments in JSON. Since:
{"a": 1, "a": 2}
Is equivalent to
{"a": 2}
You can do something like:
{
"devDependencies": "'mocha' not needed as should be globally installed",
"devDependencies" : {
"should": "*"
}
}
I have a funny hack idea.
Create an npm package name suitably as a comment divider for dependencies
and devDependencies
block in file package.json, for example x----x----x
{
"name": "app-name",
"dependencies": {
"x----x----x": "this is the first line of a comment",
"babel-cli": "6.x.x",
"babel-core": "6.x.x",
"x----x----x": "this is the second line of a comment",
"knex": "^0.11.1",
"mocha": "1.20.1",
"x----x----x": "*"
}
}
NOTE: You must add the last comment divider line with a valid version, like *
in the block.
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",
}
This has recently been discussed on the Node.js mailing list.
According to Isaac Schlueter who created npm:
... the "//" key will never be used by npm for any purpose, and is reserved for comments ... If you want to use a multiple line comment, you can use either an array, or multiple "//" keys.
When using your usual tools (npm, yarn, etc.), multiple "//" keys will be removed. This survives:
{ "//": [
"first line",
"second line" ] }
This will not survive:
{ "//": "this is the first line of a comment",
"//": "this is the second line of the comment" }
So far, most "hacks" here suggest to abuse JSON. But instead, why not abuse the underlying scripting language?
Edit The initial response was putting the description on the right using # add comments here
to wrap it; however, this does not work on Windows, because flags (e.g., npm run myframework -- --myframework-flags
) would be ignored. I changed my response to make it work on all platforms, and added some indents for readability purposes.
{
"scripts": {
"help": " echo 'Display help information (this screen)'; npm run",
"myframework": "echo 'Run myframework binary'; myframework",
"develop": " echo 'Run in development mode (with terminal output)'; npm run myframework"
"start": " echo 'Start myFramework as a daemon'; myframework start",
"stop": " echo 'Stop the myFramework daemon'; myframework stop"
"test": "echo \"Error: no test specified\" && exit 1"
}
}
This will:
npm run myframework -- --help
npm run
(which is the actual command to run to get information about available scripts)package.json
(using less
or your favorite IDE)I do something that's some of you might like:
This // inside of the name means it's a comment for me:
"//":"Main and typings are used till ES5",
"//main": "build/index",
"//typings": "build/index",