DEV Community

Tsuyoshi Tokuda
Tsuyoshi Tokuda

Posted on

2

npm version でできること

最近 npm version というコマンドを教えてもらいました。
npm は Node.js のバージョン管理ツールで、npm install くらいしか使っていないので、こういう使い方があるんだぁと感心しました。そもそもこのコマンド自体を教えてもらって初めて知りました。
ここでは、npm version の使い方を紹介します(解説は公式ドキュメントを元に行います)。

npm version

今手元の環境で npm version を実行すると以下のような出力が得られます。

$ npm version
{
  npm: '6.9.0',
  ares: '1.15.0',
  brotli: '1.0.7',
  cldr: '35.1',
  http_parser: '2.8.0',
  icu: '64.2',
  llhttp: '1.1.1',
  modules: '72',
  napi: '4',
  nghttp2: '1.38.0',
  node: '12.1.0',
  openssl: '1.1.1b',
  tz: '2019a',
  unicode: '12.1',
  uv: '1.28.0',
  v8: '7.4.288.21-node.16',
  zlib: '1.2.11'
}
Enter fullscreen mode Exit fullscreen mode

バージョンの更新

npm version は、サブコマンドで以下のものを渡して実行することでバージョンをインクリメントすることができます。

Vue.js のリポジトリを手元にダウンロードしてきてリポジトリのルートで実行してみます。

$ npm version
{
  vue: '2.6.10',
  npm: '6.9.0',
  ares: '1.15.0',
  brotli: '1.0.7',
  cldr: '35.1',
  http_parser: '2.8.0',
  icu: '64.2',
  llhttp: '1.1.1',
  modules: '72',
  napi: '4',
  nghttp2: '1.38.0',
  node: '12.1.0',
  openssl: '1.1.1b',
  tz: '2019a',
  unicode: '12.1',
  uv: '1.28.0',
  v8: '7.4.288.21-node.16',
  zlib: '1.2.11'
}
Enter fullscreen mode Exit fullscreen mode

Git リポジトリ内でじっこうすること、先程のものから package.json に記載したパッケージのバージョンの表記も追加されています。

このバージョンをインクリメントすることができます。
バージョンの仕様は semver に準拠していて、対象のバージョンナンバーを指定して以下のように実行します(引き続き Vue.js のリポジトリで行っています)。

$ npm version patch
v2.6.11
Enter fullscreen mode Exit fullscreen mode

バージョン情報を出力してみると:

$ npm version
{
  vue: '2.6.11',
  npm: '6.9.0',
  ares: '1.15.0',
  brotli: '1.0.7',
  cldr: '35.1',
  http_parser: '2.8.0',
  icu: '64.2',
  llhttp: '1.1.1',
  modules: '72',
  napi: '4',
  nghttp2: '1.38.0',
  node: '12.1.0',
  openssl: '1.1.1b',
  tz: '2019a',
  unicode: '12.1',
  uv: '1.28.0',
  v8: '7.4.288.21-node.16',
  zlib: '1.2.11'
}
Enter fullscreen mode Exit fullscreen mode

バージョンのインクリメント以外に、Gitのコミット、タグ付けも行われているので確認してみます。

$ git log
commit 2a5fabccf2199ba09ac5f32f0b73df0bca5462be (HEAD -> dev, tag: v2.6.11)
Author: tokuda109 <example@gmail.com>
Date:   Tue Oct 22 09:32:58 2019 +0900

    2.6.11

... (省略)
Enter fullscreen mode Exit fullscreen mode

コミット履歴があります。
コミットメッセージを指定する場合は以下のようにします。

$ npm version patch -m "Upgrade to %s for reasons"
v2.6.11
Enter fullscreen mode Exit fullscreen mode

%s の部分にバージョンが展開されてコミットされます。

$ git log
commit 8762348dfa78e420314158c14530fd43241e7d29 (HEAD -> dev, tag: v2.6.11)
Author: tokuda109 <example@gmail.com>
Date:   Tue Oct 22 09:38:51 2019 +0900

    Upgrade to 2.6.11 for reasons

Enter fullscreen mode Exit fullscreen mode

以下を実行してタグの一覧を確認してみます。

$ git tag
0.10.0-rc
0.11.0
... (省略)
v2.6.10
v2.6.11
... (省略)
Enter fullscreen mode Exit fullscreen mode

タグ付けもされていることが確認できました。

次にメジャーバージョンを上げてみます。

$ npm version major
v3.0.0
Enter fullscreen mode Exit fullscreen mode

再度バージョン情報を出力してみると:

$ npm version
{
  vue: '3.0.0',
  npm: '6.9.0',
  ares: '1.15.0',
  brotli: '1.0.7',
  cldr: '35.1',
  http_parser: '2.8.0',
  icu: '64.2',
  llhttp: '1.1.1',
  modules: '72',
  napi: '4',
  nghttp2: '1.38.0',
  node: '12.1.0',
  openssl: '1.1.1b',
  tz: '2019a',
  unicode: '12.1',
  uv: '1.28.0',
  v8: '7.4.288.21-node.16',
  zlib: '1.2.11'
}
Enter fullscreen mode Exit fullscreen mode

期待どおりの動作をしていることが確認できました。

今回紹介した以外にも以下のバージョンナンバーを指定してインクリメントすることができます。

  • major (v2.6.10 -> v3.0.0)
  • minor (v2.6.10 -> v2.7.0)
  • patch (v2.6.10 -> v2.6.11)
  • premajor (v2.6.10 -> v3.0.0-0)
  • preminor (v2.6.10 -> v2.7.0-0)
  • prepatch (v2.6.10 -> v2.6.11-0)

npm version prerelease

npm version prereleasepremajorpreminorprepatch によって付与されたプレリリースバージョンをインクリメントするためのサブコマンドです。

$ npm version prepatch
v2.6.11-0
Enter fullscreen mode Exit fullscreen mode

次に以下のように実行してみます:

$ npm version prerelease
v2.6.11-1
Enter fullscreen mode Exit fullscreen mode

参考

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay