概要
npmのバージョンを上げた(v6.x.x)ところ、以下のissueと同じケースに当たってCircleCIのキャッシュが効かない問題が起きました。
具体的には
- 開発ではMacを使用している
- 開発用MacとCircleCIのNode.js、npmバージョンは同一である
- CircleCIで
package-lock.json
を元にnpm install
すると、package-lock.json
が修正される package-lock.json
のハッシュ値が変わりキャッシュが使えない
という現象です。同一バージョンなのになんでやねん、という状況。
旧バージョン(v5.6.0)のnpmであれば上記の現象は起きなかったため自分の環境をそれにすればいいわけですが、他のメンバーが加わった時に同じ問題が起きうるのでnpmのバージョンを固定する方法を探してみました。
方法
1. package.jsonにengine
を追加
以下の記述を加えます。セマンティックバージョンで書けます。
"engines": { "node": "~8.11.0", "npm": "5.6.0" },
2. .npmrc
を追加
以下の記述の.npmrc
をディレクトリルートに用意します。
engine-strict=true
これで準備は完了です。
動作検証
node, npmのバージョンが一致している場合
engine
通りのバージョンの場合、
$ npm install ... added 1690 packages in 48.865s
普通に依存ライブラリのインストールが完了します。
Node.jsのバージョンが違う場合
Node.jsのバージョンを変えてみます。
$ npm install npm ERR! code ENOTSUP npm ERR! notsup Unsupported engine for account-web@1.15.0: wanted: {"node":"~8.11.0","npm":"5.6.0"} (current: {"node":"6.14.2","npm":"5.6.0"}) npm ERR! notsup Not compatible with your version of node/npm: account-web@1.15.0 npm ERR! notsup Not compatible with your version of node/npm: account-web@1.15.0 npm ERR! notsup Required: {"node":"~8.11.0","npm":"5.6.0"} npm ERR! notsup Actual: {"npm":"5.6.0","node":"6.14.2"}
エラーが出ました。
npmバージョンが違う場合
npmのバージョンを変えてみます。
$ npm install npm ERR! code ENOTSUP npm ERR! notsup Unsupported engine for account-web@1.15.0: wanted: {"node":"~8.11.0","npm":"5.6.0"} (current: {"node":"8.11.1","npm":"6.4.1"}) npm ERR! notsup Not compatible with your version of node/npm: account-web@1.15.0 npm ERR! notsup Not compatible with your version of node/npm: account-web@1.15.0 npm ERR! notsup Required: {"node":"~8.11.0","npm":"5.6.0"} npm ERR! notsup Actual: {"npm":"6.4.1","node":"8.11.1"}
エラーが出ました。
まとめ
npmは5系からpackage-lock.jsonができて依存ライブラリのバージョン固定ができるようになりました。
しかし実行環境によってフォーマットが変わるケースが在り、僕のようにCircleCIでキャッシュが効かない、といったケースに遭う可能性があるのでご注意ください。