package.json で Node.js/npm のバージョンをチェックする

はじめに

qiita.com

このエントリは Node.js Adventcalendar の 13 日目のエントリです。

npm3 と npm2 の互換性

npm2で運用しているプロジェクトでshrinkwrap.jsonが壊れた時、npm3にシュッと移行する手立てはあるか?npmのバージョンを切り替える毎にnode_modulesを作りなおすのは大変だ」と追加質問したんですが、英語が全然しゃべれなくてちゃんと通じたか疑問です……。 maybekatの回答は「そうだね、npm自身も同じ問題に直面したよ。npm2ではじめたプロジェクトはnpm2で頑張り続けた方がいいし、もしnpm3に上げるならnpm2に戻ることは考えたらいけないよ

http://amagitakayosi.hatenablog.com/entry/2015/11/12/023805#NodeDiscuss

東京Node学園祭2015で登壇された amagitakayosi さんのブログエントリからの引用です

現在 Node.js の stable 版は v5.x で npm3、Long-term Support(LTS) の最新版は v4.x 系で npm2 となっています

Mac 環境での Node.js

Mac 上で開発していたとしても個々の好みで homebrew、MacPorts や nvm、nodebrew 等を使ってインストールしているかもしれないですよね

現在の状況は以下の様になっています

package.json の node-version/npm-version

残念ながらこちらはインストールする依存モジュールのチェックに使われているのみで npm install 時に実行している Node.js や npm のバージョンをチェックしているものでは無いようです

Search Results · GitHub

チーム開発時には Node.js と npm のバージョンはそろえるべきなのでその辺の自動的なチェックは出来ないようです

check-engines

www.npmjs.com

このモジュールを使うと package.json に書かれた node-version/npm-version をチェックしてくれます
しかし global にインストールしないとコマンドとして実行できません

これを解決するには package.json に devDependencies として定義してインストールした後に postinstall でチェックを実行するという強引な手法が使えます
でもこれエレガントではないですよね…

➜  npm_test  cat package.json
{
  "name": "npm_test",
  "version": "0.0.1",
  "description": "",
  "main": "index.js",
  "engines": {
    "node": "5.x",
    "npm": "3.x"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "postinstall": "check-engines"
  },
  "keywords": [],
  "author": "kysnm <tokyoincidents.g@gmail.com> (http://kysnm.hatenablog.com/)",
  "license": "MIT",
  "devDependencies": {
    "check-engines": "^1.2.0"
  }
}
➜  npm_test  npm i
npm WARN package.json npm_test@0.0.1 No description
npm WARN package.json npm_test@0.0.1 No repository field.
npm WARN package.json npm_test@0.0.1 No README data

> npm_test@0.0.1 postinstall /Users/jps/work/node/npm_test
> check-engines

[Error: Error: node version (4.2.1) does not satisfy specified range (5.x)
Error: npm version (2.14.7) does not satisfy specified range (3.x)]

npm ERR! Darwin 13.4.0
npm ERR! argv "/Users/jps/.nodebrew/node/v4.2.1/bin/node" "/Users/jps/.nodebrew/current/bin/npm" "i"
npm ERR! node v4.2.1
npm ERR! npm  v2.14.7
npm ERR! code ELIFECYCLE
npm ERR! npm_test@0.0.1 postinstall: `check-engines`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the npm_test@0.0.1 postinstall script 'check-engines'.
npm ERR! This is most likely a problem with the npm_test package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     check-engines
npm ERR! You can get their info via:
npm ERR!     npm owner ls npm_test
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /Users/jps/work/node/npm_test/npm-debug.log

Ruby の bundler

Bundler: The best way to manage a Ruby application's gems

➜  bundler_test  cat Gemfile
# A sample Gemfile
source "https://rubygems.org"

ruby "2.1"

gem "pry"

➜  bundler_test  bundle
Your Ruby version is 2.2.3, but your Gemfile specified 2.1

一方 Ruby ではバージョンチェックする仕組みがあります
出来ればこういう風にしたいですね

おわりに

本当は変更箇所のふわっとした当たりはついているので出来ればパッチを送りたかったんですが、npm へのパッチは初めての事になるし、ちょっと時間がかかりそうなので今回は見送りました
時間を見つけてパッチは書きたいなと思っています

もっと良い方法をご存知の方がいらっしゃったらぜひ教えて下さい!