我有一个简单的包裹。json文件,我想添加一个评论。有办法做到这一点吗,或者有什么hack可以做到这一点吗?
{
"name": "My Project",
"version": "0.0.1",
"private": true,
"dependencies": {
"express": "3.x",
"mongoose": "3.x"
},
"devDependencies" : {
"should": "*"
/* "mocha": "*" not needed as should be globally installed */
}
}
上面的示例注释在npm崩溃时不起作用。我还尝试了// style注释。
最近在Node.js邮件列表中讨论了这个问题。
根据创建npm的Isaac Schlueter的说法:
... "//"键永远不会被NPM用于任何目的,它是为注释保留的…如果想使用多行注释,可以使用数组或多个“//”键。
当使用你常用的工具(npm, yarn等)时,多个“//”键将被移除。这生存:
{ "//": [
"first line",
"second line" ] }
这将不复存在:
{ "//": "this is the first line of a comment",
"//": "this is the second line of the comment" }
必须注意,"//"只能在包的根目录下使用。json对象。例如
{
“/ /”:“评论!”
“依赖”:{…}
}
是有效的但是
{
“依赖”:{
“/ /”:“评论?”
}
}
是无效的。
——@david_p评论
我一直在这样做:
{
...
"scripts": {
"about": "echo 'Say something about this project'",
"about:clean": "echo 'Say something about the clean script'",
"clean": "do something",
"about:build": "echo 'Say something about building it'",
"build": "do something",
"about:watch": "echo 'Say something about how watch works'",
"watch": "do something",
}
...
}
这样,我既可以读取脚本本身的“伪注释”,也可以运行如下代码,在终端中查看某种帮助:
npm run about
npm run about:watch
如果你用的是纱线就更好了。
yarn about:clean
此外,正如@Dakota Jang在评论中指出的那样,你可以使用//之类的键来更清楚地表明这是一条评论。
像这样:
{
...
"scripts": {
"//clean": "echo 'Say something about the clean script'",
"clean": "do something",
"//build": "echo 'Say something about building it'",
"build": "do something",
"//watch": "echo 'Say something about how watch works'",
"watch": "do something",
}
...
}
然后运行:
npm run //build
# or
yarn //build
在终端中会有一个helper输出,在包中会有一个“comment”。Json也是。