Home > Software engineering >  Order of fields in package.json?
Order of fields in package.json?

Time:08-30

Recently I made a PR to a repo to add a typings field in package.json, and the maintainer proposed the following change:

-   "typings": "./src/index.d.ts",
-   "main": "./src/index.js",
-   "exports": "./src/index.js",
    "main": "./src/index.js",
    "exports": "./src/index.js",
    "types": "./src/index.d.ts",

I wonder if there is a recommended rule or convention of orders of entries in package.json.

CodePudding user response:

It could be that the maintainer has a formating tool enabled like prettier that automatically sorts the package.json file. Or maybe they just like ordering their scripts in order of execution. Either way it's a json object so ultimately the order of the fields does not matter, just the nesting.

Another thought is they could be using a special batch or shell script that hand cranks the package.json during CD, converting the scripts object to an array that gets run in order of execution. Without seeing the github I can't be sure though.

https://www.npmjs.com/package/prettier-package-json

  • Related