如何为package.json添加注释以进行npm安装?

本文讨论了如何在package.json文件中添加注释,包括使用Isaac Schlueter建议的"//"键,利用JSON中重复键的特性,以及通过创建特殊的npm包名作为注释分隔符等方法。同时,提到了一些技巧的潜在问题,如多行注释可能被删除,以及使用脚本替代直接在package.json中添加注释。
摘要由CSDN通过智能技术生成

本文翻译自:How do I add comments to package.json for npm install?

I've got a simple package.json file and I want to add a comment. 我有一个简单的package.json文件,我想添加一条注释。 Is there a way to do this, or are there any hacks to make this work? 有没有办法做到这一点,或者有什么技巧可以做到这一点?

{
  "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 */
  }
}

The example comment above doesn't work as npm breaks. 上面的示例注释在npm中断时不起作用。 I've also tried // style comments. 我也尝试过//样式注释。


#1楼

参考:https://stackoom.com/question/xfgJ/如何为package-json添加注释以进行npm安装


#2楼

This has recently been discussed in the node.js mailing list . 最近在node.js邮件列表中对此进行了讨论。

According to Isaac Schlueter who created npm: 根据创建npm的Isaac Schlueter的说法:

... 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. ... npm绝不会将“ //”键用于任何目的,并且仅用于注释...如果要使用多行注释,则可以使用数组,也可以使用多个“ //”键。

When using your usual tools (npm, yarn, etc) multiple "//" keys will be removed. 使用常用工具(npm,yarn等)时,多个“ //”键将被删除。 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" } 

#3楼

Here is another hack for adding comments in JSON. 这是在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": "*"
  }
}

#4楼

You can always abuse the fact that duplicated keys are overwritten. 您总是可以滥用重复密钥被覆盖的事实。 This is what I just wrote: 这就是我刚刚写的:

"dependencies": {
  "grunt": "...",
  "grunt-cli": "...",

  "api-easy": "# Here is the pull request: https://github.com/...",
  "api-easy": "git://..."

  "grunt-vows": "...",
  "vows": "..."
}

However, it is not clear whether JSON allows duplicated keys (see Does JSON syntax allow duplicate keys in an object? . It seems to work with npm, so I take the risk. 但是,尚不清楚JSON是否允许重复键(请参阅JSON语法是否允许对象中重复键? 。它似乎可以与npm配合使用,所以我冒险。

The recommened hack is to use "//" keys (from the nodejs mailing list ). 推荐的技巧是使用"//"键(来自nodejs邮件列表 )。 When I tested it, it did not work with "dependencies" sections, though. 当我对其进行测试时,它不适用于“依赖项”部分。 Also, the example in the post uses multiple "//" keys, which implies that npm does not reject JSON files with duplicated keys. 此外,帖子中的示例使用多个"//"键,这意味着npm不会拒绝具有重复键的JSON文件。 In other words, the hack above should always be fine. 换句话说,上面的技巧应该总是可以的。

Update: One annoying disadvantage of the duplicated key hack is that npm install --save silently eliminates all duplicates. 更新:重复密钥破解的一个令人讨厌的缺点是npm install --save静默消除了所有重复项。 Unfortunately, it is very easy to overlook it and your well-intentioned comments are gone. 不幸的是,很容易忽略它,而您的好意也就消失了。

The "//" hack is still the safest as it seems. "//"骇客似乎仍然是最安全的。 However, multi-line comments will be removed by npm install --save , too. 但是,多行注释也将被npm install --save删除。


#5楼

Here's my take on comments within package.json / bower.json : 这是我对package.json / bower.json评论的bower.json

I have package.json.js that contains a script that exports the actual package.json . 我有package.json.js ,其中包含一个导出实际package.json的脚本。 Running the script overwrites the old package.json and tells me what changes it made, perfect to help you keep track of automatic changes npm made. 运行脚本会覆盖旧的package.json并告诉我它进行了哪些更改,非常适合帮助您跟踪npm进行的自动更改。 That way I can even programatically define what packages I want to use. 这样,我什至可以以编程方式定义我要使用的软件包。

The latest grunt task is here: https://gist.github.com/MarZab/72fa6b85bc9e71de5991 最新的grunt任务在这里: https : //gist.github.com/MarZab/72fa6b85bc9e71de5991


#6楼

I have a funny hack idea. 我有一个有趣的技巧。

Create npm package name suitably as comment divider for dependencies and devDependencies block in package.json, for example x----x----x 适当地创建npm包名称作为package.json中dependenciesdevDependencies块的注释分隔符,例如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 : Must add last comment divider line with valid version like * in block. 注意 :必须在块中添加带有有效版本(如*最后一个注释分隔线。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值