Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

node.js - Npm install ignores tilde (~) in version number

I would like to install 1.8.x version a package, and be able to later automatically update this dependency inside the >=1.8.0 <1.9.0 range.

I tried to run this command:

npm install example-package@~1.8 --save

Unfortunately it adds this record to my package.json:

"example-package" : "^1.8.0"

But what I want is this:

"example-package" : "~1.8.0"

How is it possible to do it with npm install, without manually edit the package.json file?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The semver prefix is defined by the save-prefix config. The default value is a caret (^) which you can check by running the following npm config command:

npm config get save-prefix

Unfortunately, the npm install command has no option to specify this, so what you'll need to do is:

  1. Set the save-prefix value to a tilde (~) by running:

    npm config set save-prefix="~"
    
  2. Install your package by running:

    npm i [email protected] --save
    

    Note: The tilde (~) must not be included in the install command.

  3. Finally, set the save-prefix value back to it's default, i.e. a caret (^) by running:

    npm config delete save-prefix
    

    Note: You wouldn't do this last step if you wanted all future npm install's to use the tilde (~) prefix instead of a caret (^).

The above steps will add the following record in package.json:

"example-package" : "~1.8.0"

Note the tilde ~ instead of the default caret ^


You can utilize the && operator to combine the aforementioned commands into a compound command. For instance:

npm config set save-prefix="~" && npm i [email protected] --save && npm config delete save-prefix

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...