Skip to content

Use same sorting logic for sorting dependencies as npm v7 #234

@G-Rath

Description

@G-Rath

NPM v7 changed the way dependencies are sorted very slightly which means this:

{
  "name": "spj-te",
  "dependencies": {
    "sort-package-json": "^1.52.0"
  },
  "devDependencies": {
    "@types/koa-bodyparser": "^4.3.3",
    "@types/koa__router": "^8.0.8"
  }
}

becomes this:

{
  "name": "spj-te",
  "dependencies": {
    "sort-package-json": "^1.52.0"
  },
  "devDependencies": {
    "@types/koa__router": "^8.0.8",
    "@types/koa-bodyparser": "^4.3.3"
  }
}

The specific change they made was to use localeCompare, which technically the better way for sorting strings than just calling sort but means npm v7 & above sort differently compared to both sort-package-json and the other two main package managers.

I opened an issue for discussion about this but it was closed with a comment about the change being intentional: npm/cli#3935

I was thinking that sort-package-json could attempt to figure out if it should use localeCompare by trying to read package-lock.json and checking if the lockfileVersion is greater than 1, otherwise falling back to its current behaviour if its 1 or the lock cannot be read.

I'm happy to implement this change.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions