Help us understand the problem. What is going on with this article?

package.json のチルダ(~) とキャレット(^)

More than 3 years have passed since last update.

バージョン指定でいっつもこいつの違いを忘れてしまって、npm とか semver のドキュメントを探すところから始まるから自分用のメモ。

  • ^1.1.2 = 1.x
  • ~1.1.2 = 1.1.x

(追記 一応↑は間違っていないのだけど、厳密にいうとちょっと間違っていた)

チルダ表記 ~

"明記したところ以下のバージョンがあがることのみ許容"

  • ~1.1.2 = 1.1.2 <= version < 1.2.0
  • ~1.1 = 1.1.x
  • ~1 = 1.x

オリジナルの定義は、

Allows patch-level changes if a minor version is specified on the comparator. Allows minor-level changes if not.

キャレット表記 ^

"一番左側にある、ゼロでないバージョニングは変えない (それ以下があがることは許容)"

  • ^1.2.3 := 1.2.3 <= version < 2.0.0
  • ^0.2.3 := 0.2.3 <= version < 0.3.0
  • ^0.0.3 := 0.0.3 <= version < 0.0.4

オリジナルの定義は

Allows changes that do not modify the left-most non-zero digit in the [major, minor, patch] tuple.

余談

こんなことで悩むくらいなら最初から 1.2.x とか >=1.0.2 <2.1.2 とかみたいに書いておいたほうが圧倒的にわかりやすい。
許容されるのは↓の通り。

docs.npmjs.com/files/package.json
{ "dependencies" :
  { "foo" : "1.0.0 - 2.9999.9999"
  , "bar" : ">=1.0.2 <2.1.2"
  , "baz" : ">1.0.2 <=2.3.4"
  , "boo" : "2.0.1"
  , "qux" : "<1.0.0 || >=2.3.1 <2.4.5 || >=2.5.2 <3.0.0"
  , "asd" : "http://asdf.com/asdf.tar.gz"
  , "til" : "~1.2"
  , "elf" : "~1.2.3"
  , "two" : "2.x"
  , "thr" : "3.3.x"
  , "lat" : "latest"
  , "dyl" : "file:../dyl"
  }
}

参考

本件ではどうでもいいけどこれも:

stafes
お弁当&ケータリングの総合モール「ごちクル」を運営。”食を通じて世界をより幸せにする”ことに情熱を注ぐ会社です。
https://stafes.co.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした