如何爲package.json添加註釋以進行npm安裝?

本文翻譯自: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. 注意 :必須在塊中添加帶有有效版本(如*最後一個註釋分隔線。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章