Lorenz Leutgeb
Lorenz Leutgeb

Reputation: 504

Is there a tool to validate/check that package.json and package-lock.json are consistent?

Sometimes people change package.json and forget to run npm i which will update package-lock.json, or package.json and package-lock.json are otherwise out of sync. This is an assumption not to be discussed/questioned here. I am looking for a tool that helps detect such cases.

Do you know of an npm feature or third-party tool that can sanity-check package-lock.json? For example, it should resolve all transitive dependencies, and check that they all are mentioned in the lock file with a version in the correct semver range. It should tell whether it would make sense to run npm i in order to update your lockfile, or also whether npm ci would get you all the dependencies required as defined in package.json (accounting for transitivity).

I thought that npm --loglevel verbose install --dry-run would be a reasonable candidate, but its output does not mention what it would do to package-lock.json in case it would be run without --dry-run. Of course one option would be to run npm i and then git diff package-lock.json (or similar), but that's dirty.

Upvotes: 10

Views: 10606

Answers (1)

Manuel Spigolon
Manuel Spigolon

Reputation: 12900

npm ls --depth 1 does these checks:

for example, this is the output:

+-- UNMET DEPENDENCY fastify@^2.0.0
+-- [email protected] extraneous
`-- [email protected]
  +-- @sindresorhus/[email protected]
  +-- @szmarczak/[email protected]
  +-- @types/[email protected]
  +-- [email protected]
  +-- [email protected]
  +-- [email protected]
  +-- [email protected]
  +-- [email protected]
  +-- [email protected]
  +-- [email protected]
  +-- [email protected]
  +-- [email protected]
  +-- [email protected]
  `-- [email protected]

npm ERR! missing: fastify@^2.0.0, required by [email protected]
npm ERR! extraneous: [email protected]

I get this doing:

npm init --yes
npm i got
npm i foo
// removed foo manually from package json
// added fastify manually to package json

Upvotes: 5

Related Questions