When you're bored, you usually have your best ideas. While wasting some time on twitter this morning I met a legit question coming from @caludio (you should really follow him):
Are there any plans to make it possible for package.json to contain comments? I can understand the implications but i.e., I would like to write the reason why I had to lock a package to a specific version (hello, TypeScript)
And I was a bit confused... It's a JSON, I can do whatever I want with it! So I've put together some code just to run an npm install
on it...
{
"name": "napolux-frontend",
"version": "1.0.0",
"description": "it's a test",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "MIT",
"devDependencies": {
"@babel/core": "^7.2.2",
"@babel/preset-env": "^7.3.1",
"babel-polyfill": "^6.26.0",
"gulp": "^4.0.0",
"gulp-babel": "^8.0.0",
"gulp-rename": "^1.4.0",
"gulp-uglify": "^3.0.1"
},
"dependencies": {
"jquery": "^3.3.1"
},
"comments": {
"dependencies": "we use jQuery because of reasons",
"repository": "our beloved repo",
"license": "we love MIT, so why not",
"devDependencies": {
"@babel/core": "it's @ version 7.2.2 because of...",
"gulp-rename": "why not"
}
}
}
It works! It's not the best possible solution, for sure no inline comments for example, but if you maintain the structure of your comments
section very close to the one of package.json
it will be definetely readable!
This post originally appeared on https://coding.napolux.com, but was brought to dev.to for your reading pleasure ❤.
Top comments (12)
The convention I have started using is the following:
Note: For the
dependencies
,devDependencies
, etc sections, the comment annotations can't be added directly above the individual package dependencies inside the configuration object sincenpm
is expecting the key to be the name of an npm package. Hence the reason for the@comment dependencies
.Note: In certain contexts, such as in the
scripts
object, some editors/IDEs may complain about the array. In thescripts
context, VS Code expects a string for the value -- not an array.A top-level comment annotation can also be added similar to what Francesco presented.
However, I like keeping the comment as close as possible to what it applies to.
I have changed a few things in how I add comments to package.json. Here is an example of the new convention I am following:
I really like this idea; it should be a standard. Let's discuss and vote up in npm / cli / issues / #677
I think you might wanna keep things like this in a separate place, like Github issues, or maybe just some file. But I don’t think
package.json
is a good place.It’s the best place: right next (oh, well, as close as possible with a file format that was not made for writing configuration) to where you do the thing. You don’t want anybody (even your future self), to think “Oh, look, someone bumped babel/preset-env to 7.3 but forgot to do the same with babel/core”. And that’s what people are going to think, not “Oh, look, babel/preset-env is on 7.3, but babel/core is on 7.2, let’s walk through all the GitHub issues to see if it’s intentional or not.”
Why do you think
package.json
is not a good place for comments about things inpackage.json
?I just feel most people wouldn’t be looking for this kind of information in
package.json
. But if you wanna put stuff in there then sure go ahead.Could be. But having them in the same file makes them super close to the packages and their definitions.
Agree, I think this is the preference of most people
Yeah I guess. Everyone has their own preferences.
This could be an interesting discussion which discussed the extending the package json file, such as using JSON5, YAML, HJSON instead of just plain JSON.
npm.community/t/support-package-js...
And most of these syntax standards support comments out of box. Love to see further result. Thank you for starting the discussion @napolux
Thanks for this - it's a solution to a problem I've had a couple of times while teaching, but it never occurred to me to try this!