1

Sometimes my team members forgot to update npm-shrinkwrap.json after update package.json. I know this package from uber, but it can not be used with npm v3. So now it is not solution.

Do have I possibility to auto check consistency for npm-shrinkwrap.json and package.json? I want to do this in git-hook or/and continuous.

galkin
  • 5,264
  • 3
  • 34
  • 51

2 Answers2

1

You can test out the npm package git-hooks, which allows to install pre-commit or pre-push hooks (that is client-side hooks)

Such hooks (like this pre-commit one) can be used to check the consistency of source files like npm-shrinkwrap.json.

See also for instance turadg/npm-shrinkwrap-git-hooks

A set of scripts to automatically npm shrinkwrap and npm install as needed.

If you stage a change to package.json, the pre-commit hook will run npm shrinkwrap to update npm-shrinkwrap.json.

#!/usr/bin/env bash

# This ensures that dependencies are installed locally whenever merging a commit
# that changed the shrinkwrap.

function package_changes_staged {
  ! git diff --cached  --quiet -- package.json
}

# update shrinkwrap when spec changes
if package_changes_staged; then
  echo "Running 'npm shrinkwrap' to match new package spec..." >&2
  npm shrinkwrap
  git add npm-shrinkwrap.json
fi

UPDATE by galk-in

I chose pre-commit with this updates in package.json

...
"scripts": {
  "check-shrinkwrap": "if (! git diff --cached  --quiet -- package.json); then echo 'Running `npm shrinkwrap` to match new package spec...' >&2; npm shrinkwrap; git add npm-shrinkwrap.json; fi"
},
...
"pre-commit": [
  "check-shrinkwrap",
  "test"
]
...
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thank you! Do you have any idea about CI solution? – galkin Nov 01 '16 at 08:49
  • @galk.in Sure. you can integrate that in a Travis CI environment for instance (https://pub.clevertech.biz/faster-node-js-builds-with-travis-ci-41aa275015e7#.dqvjoo41f) – VonC Nov 01 '16 at 08:59
  • @galk.in you can see your issue discussed in the context of another CI: greekeeper: https://github.com/greenkeeperio/greenkeeper/issues/96 – VonC Nov 01 '16 at 09:01
1

Update from 24 jun 2017 The modern answer is to use npm 5 with package-lock.json

galkin
  • 5,264
  • 3
  • 34
  • 51