欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

npm生成的package.json文件中依赖项版本号前的波浪号(~)和插入符号(^)是啥意思? 有大用 有大大用

  • npm生成的package.json文件中依赖项版本号前的波浪号(~)和插入符号(^)是啥意思?

    package.json一般也就长这么模样:

    {
      "name": "",
      "description": "",
      "version": "4.17.1",
      "author": "",
      "contributors": [],
      "license": "MIT",
      "repository": "",
      "homepage": "",
      "keywords": [],
      "dependencies": {
        "accepts": "~1.3.7",
        "array-flatten": "1.1.1",
        "body-parser": "^1.19.0",
        "content-disposition": "0.5.3",
        "content-type": "~1.0.4",
        "cookie": "^0.4.0",
        "cookie-signature": "1.0.6",
        "debug": "2.6.9",
        "depd": "~1.1.2",
        "encodeurl": "~1.0.2"
      },
      "devDependencies": {},
      "engines": {
        "node": ">= 0.10.0"
      },
      "files": [],
      "scripts": {
        "lint": "eslint .",
        "test": "mocha --require test/support/env --reporter spec --bail --check-leaks test/ test/acceptance/",
        "test-ci": "...",
        "test-cov": "...",
        "test-tap": "mocha --require test/support/env --reporter tap --check-leaks test/ test/acceptance/"
      }
    }

    其中十分重要的一项就是dependencies,也就是依赖项,每次 npm install 的时候下载下来的东东都是根据这里来的。

    很显然这个dependencies对象中的key是依赖项的名称,值则是版本号,但是版本号前面有的啥也没有,比如:“array-flatten”: “1.1.1”,有的有波浪号,比如:“accepts”: “~1.3.7”,也有的有插入符号,比如:“body-parser”: “^1.19.0”。那这些到底有啥区别呢?

    其实也比较简单:

    波浪号〜匹配最新补丁版本号,也就是版本号的第三个数字。比如~1.2.3将匹配所有1.2.x版本,但将在1.3.0上停止。

    插入符号^ 更宽松。 它匹配的是最新次要版本号,也就是第二个数字。比如:^ 1.2.3将匹配任何1.x.x版本,包括1.3.0,但将在2.0.0上停止。

    前面啥符号也没有,很显然意思就是确定唯一指定的版本号。

    当然这里还可以写>,>=,<,<=,比如:

    "dependencies": {
        "accepts": "~1.3.7",
        "array-flatten": "1.1.1",
        "body-parser": "^1.19.0",
        "content-disposition": ">0.5.3",
        "content-type": "~1.0.4",
        "cookie": "^0.4.0",
        "cookie-signature": "<1.0.6",
        "debug": ">=2.6.9",
        "depd": "~1.1.2",
        "encodeurl": "<1.0.2"
      }

    意思也很简单,就是大于,或者大于等于,小于或者小于等于后面的版本号。

    如果前面是星号*,那意思就是匹配任何版本。

    如果版本号的值是latest,那意思安装的永远是最新发布的版本。

    如果只匹配版本号的其中一位,除了上面的做法外,还有一种,那就是用x,比如:1.2.x,就可以匹配1.2.1,1.2.2,…,但是1.3.0肯定是不行的,这大家都懂,不用多解释。

    最后再举个栗子吧:

    "dependencies": {
        "accepts": "1.3.x"
      }

     


    原文链接:https://blog.csdn.net/sinat_36246371/article/details/94621109


来自  http://t.zoukankan.com/smile-fanyin-p-12514490.html


普通分类: