How do I add comments to package.json for npm install?

后端 未结 20 1340
悲哀的现实
悲哀的现实 2020-12-07 07:30

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?



        
相关标签:
20条回答
  • 2020-12-07 07:39

    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": "*"
      }
    }
    
    0 讨论(0)
  • 2020-12-07 07:40

    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.

    0 讨论(0)
  • 2020-12-07 07:40

    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",
    }
    
    0 讨论(0)
  • 2020-12-07 07:41

    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" }
    
    0 讨论(0)
  • 2020-12-07 07:41

    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:

    1. Not break JSON compliance (or at least it's not a hack, and your IDE will not give you warnings for doing strange, dangerous stuff)
    2. Works cross platform (tested on macOS and Windows, assuming it would work just fine on Linux)
    3. Does not get in the way of running npm run myframework -- --help
    4. Will output meaningful info when running npm run (which is the actual command to run to get information about available scripts)
    5. Presents a more explicit help command (in case some developers are not aware that npm run presents such output)
    6. Will show both the commands and its description when running the command itself
    7. Is somewhat readable when just opening package.json (using less or your favorite IDE)
    0 讨论(0)
  • 2020-12-07 07:42

    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",
    
    0 讨论(0)
提交回复
热议问题