如何检查一致性 npm-shrinkwrap.json 和 package.json

How check consistency npm-shrinkwrap.json and package.json

有时我的团队成员在更新 package.json 后忘记更新 npm-shrinkwrap.json。我从 uber 知道 this package,但它不能与 npm v3 一起使用。所以现在不是解决方案。

是否可以自动检查 npm-shrinkwrap.json 和 package.json 的一致性?我想在 git-hook or/and 中连续执行此操作。

您可以测试 npm package git-hooks, which allows to install pre-commit or pre-push hooks (that is client-side hooks)

此类钩子(如pre-commit one)可用于检查源文件的一致性,如npm-shrinkwrap.json

另见 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

更新 由 galk-in

我在 package.json

中选择 pre-commit 进行此更新
...
"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"
]
...

2017 年 6 月 24 日更新 现代的答案是将 npm 5 与 package-lock.json

一起使用