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

后端 未结 20 1342
悲哀的现实
悲哀的现实 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:55

    For npm's package.json, I have found two ways (after reading this conversation):

      "devDependencies": {
        "del-comment": [
          "some-text"
        ],
        "del": "^5.1.0 ! inner comment",
        "envify-comment": [
          "some-text"
        ],
        "envify": "4.1.0 ! inner comment"
      }
    

    But with the update or reinstall of package with "--save" or "--save-dev, a comment like "^4.1.0 ! comment" in the corresponding place will be deleted. And all this will break npm audit.

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

    As duplicate comment keys are removed running package.json tools (npm, yarn, etc.), I came to using a hashed version which allows for better reading as multiple lines and keys like:

    "//": {
      "alpaca": "we use the bootstrap version",
      "eonasdan-bootstrap-datetimepicker": "instead of bootstrap-datetimepicker",
      "moment-with-locales": "is part of moment"
    },
    

    which is 'valid' according to my IDE as a root key, but within dependencies it complains expecting a string value.

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

    My take on the frustration of no comments in JSON. I create new nodes, named for the nodes they refer to, but prefixed with underscores. This is imperfect, but functional.

    {
      "name": "myapp",
      "version": "0.1.0",
      "private": true,
      "dependencies": {
        "react": "^16.3.2",
        "react-dom": "^16.3.2",
        "react-scripts": "1.1.4"
      },
      "scripts": {
        "__start": [
            "a note about how the start script works"
        ],
        "start": "react-scripts start",
        "build": "react-scripts build",
        "test": "react-scripts test --env=jsdom",
        "eject": "react-scripts eject"
      },
      "__proxy": [
        "A note about how proxy works",
        "multilines are easy enough to add"
      ],
      "proxy": "http://server.whatever.com:8000"
    }
    
    0 讨论(0)
  • 2020-12-07 08:02

    After wasting an hour on complex and hacky solutions, I've found both simple and valid solution for commenting my bulky dependencies section in package.json. Just like this:

    {
      "name": "package name",
      "version": "1.0",
      "description": "package description",
      "scripts": {
        "start": "npm install && node server.js"
      },
      "scriptsComments": {
        "start": "Runs development build on a local server configured by server.js"
      },
      "dependencies": {
        "ajv": "^5.2.2"
      },
      "dependenciesComments": {
        "ajv": "JSON-Schema Validator for validation of API data"
      }
    }
    

    When sorted the same way, it's now very easy for me to track these pairs of dependencies/comments either in Git commit diffs or in an editor while working with file package.json.

    And no extra tools are involved, just plain and valid JSON.

    0 讨论(0)
  • 2020-12-07 08:02

    I ended up with a scripts like that:

      "scripts": {
        "//-1a": "---------------------------------------------------------------",
        "//-1b": "---------------------- from node_modules ----------------------",
        "//-1c": "---------------------------------------------------------------",
        "ng": "ng",
        "prettier": "prettier",
        "tslint": "tslint",
        "//-2a": "---------------------------------------------------------------",
        "//-2b": "--------------------------- backend ---------------------------",
        "//-2c": "---------------------------------------------------------------",
        "back:start": "node backend/index.js",
        "back:start:watch": "nodemon",
        "back:build:prod": "tsc -p backend/tsconfig.json",
        "back:serve:prod": "NODE_ENV=production node backend/dist/main.js",
        "back:lint:check": "tslint -c ./backend/tslint.json './backend/src/**/*.ts'",
        "back:lint:fix": "yarn run back:lint:check --fix",
        "back:check": "yarn run back:lint:check && yarn run back:prettier:check",
        "back:check:fix": "yarn run back:lint:fix; yarn run back:prettier:fix",
        "back:prettier:base-files": "yarn run prettier './backend/**/*.ts'",
        "back:prettier:fix": "yarn run back:prettier:base-files --write",
        "back:prettier:check": "yarn run back:prettier:base-files -l",
        "back:test": "ts-node --project backend/tsconfig.json node_modules/jasmine/bin/jasmine ./backend/**/*spec.ts",
        "back:test:watch": "watch 'yarn run back:test' backend",
        "back:test:coverage": "echo TODO",
        "//-3a": "---------------------------------------------------------------",
        "//-3b": "-------------------------- frontend ---------------------------",
        "//-3c": "---------------------------------------------------------------",
        "front:start": "yarn run ng serve",
        "front:test": "yarn run ng test",
        "front:test:ci": "yarn run front:test --single-run --progress=false",
        "front:e2e": "yarn run ng e2e",
        "front:e2e:ci": "yarn run ng e2e --prod --progress=false",
        "front:build:prod": "yarn run ng build --prod --e=prod --no-sourcemap --build-optimizer",
        "front:lint:check": "yarn run ng lint --type-check",
        "front:lint:fix": "yarn run front:lint:check --fix",
        "front:check": "yarn run front:lint:check && yarn run front:prettier:check",
        "front:check:fix": "yarn run front:lint:fix; yarn run front:prettier:fix",
        "front:prettier:base-files": "yarn run prettier \"./frontend/{e2e,src}/**/*.{scss,ts}\"",
        "front:prettier:fix": "yarn run front:prettier:base-files --write",
        "front:prettier:check": "yarn run front:prettier:base-files -l",
        "front:postbuild": "gulp compress",
        "//-4a": "---------------------------------------------------------------",
        "//-4b": "--------------------------- cypress ---------------------------",
        "//-4c": "---------------------------------------------------------------",
        "cy:open": "cypress open",
        "cy:headless": "cypress run",
        "cy:prettier:base-files": "yarn run prettier \"./cypress/**/*.{js,ts}\"",
        "cy:prettier:fix": "yarn run front:prettier:base-files --write",
        "cy:prettier:check": "yarn run front:prettier:base-files -l",
        "//-5a": "---------------------------------------------------------------",
        "//-5b": "--------------------------- common ----------------------------",
        "//-5c": "---------------------------------------------------------------",
        "all:check": "yarn run back:check && yarn run front:check && yarn run cy:prettier:check",
        "all:check:fix": "yarn run back:check:fix && yarn run front:check:fix && yarn run cy:prettier:fix",
        "//-6a": "---------------------------------------------------------------",
        "//-6b": "--------------------------- hooks -----------------------------",
        "//-6c": "---------------------------------------------------------------",
        "precommit": "lint-staged",
        "prepush": "yarn run back:lint:check && yarn run front:lint:check"
      },
    

    My intent here is not to clarify one line, just to have some sort of delimiters between my scripts for backend, frontend, all, etc.

    I'm not a huge fan of 1a, 1b, 1c, 2a, ... but the keys are different and I do not have any problem at all like that.

    0 讨论(0)
  • 2020-12-07 08:02

    I like this:

      "scripts": {
        "⏬⏬⏬ Jenkins Build - in this order ⏬⏬⏬                                                                                                  ": "",
        "purge": "lerna run something",
        "clean:test": "lerna exec --ignore nanana"
    }
    

    There are extra spaces in the command name, so in Visual Studio Code's NPM Scripts plugin you have a better look.

    0 讨论(0)
提交回复
热议问题