简体   繁体   English

如何强制npm默认安装特定版本的包?

[英]How to force npm to install specific version of package by default?

Is there a way to config npm not to install package using a lazy version like: 有没有办法配置npm不使用惰性版本安装包,如:

"coffee-script": "^1.11.1",

But

"coffee-script": "1.11.1",

And have this behavior become the default one? 并将此行为成为默认行为? We usually don't want to use lazy versions, I prefer to manually upgrade everything to the latest from time to time rather than having a bug thrown up at my face after a new deployment due to a bug in one of my dependencies. 我们通常不想使用懒惰的版本,我更喜欢手动将所有内容升级到最新版本,而不是由于我的某个依赖项中的错误而在新部署后在我的脸上抛出了一个错误。

The only way to "do that" right now is to manually remove the ^ character every time after every npm install , which is a bit boring. 现在“执行此操作”的唯一方法是每次npm install后手动删除^字符,这有点无聊。

This is the command that will set a user variable in your npm configuration to always default to use the exact version when performing an npm installation of all packages. 这是一个命令,它将npm配置中的用户变量设置为在执行所有软件包的npm安装时始终默认使用完全版本

Enter this command in your terminal: 在终端中输入以下命令:

npm config set save-exact=true

The new preference is stored in a user-based custom npm configuration file. 新首选项存储在基于用户的自定义npm配置文件中。 It is located here: 它位于这里:

~/.npmrc

Finally, you can verify the setting was saved with the command: 最后,您可以使用以下命令验证设置是否已保存:

npm config ls

Official NPM docs here: 官方NPM文件在这里:

https://docs.npmjs.com/misc/config https://docs.npmjs.com/misc/config

https://docs.npmjs.com/files/npmrc https://docs.npmjs.com/files/npmrc

I'd suggest using npm shrinkwrap . 我建议使用npm shrinkwrap It will create npm-shrinkwrap.json which specifies the currently used versions of your dependencies and then npm install will honor that. 它将创建npm-shrinkwrap.json,它指定当前使用的依赖项版本,然后npm install将尊重它。

Then, when you want to upgrade all of your dependencies, delete the npm-shrinkwrap.json file, run npm install and re-run npm shrinkwrap. 然后,当您要升级所有依赖项时,删除npm-shrinkwrap.json文件,运行npm install并重新运行npm shrinkwrap。 Or, to upgrade a single package dependency, run npm install --save <package-name> . 或者,要升级单个包依赖项,请运行npm install --save <package-name> This will update the npm-shrinkwrap file with that updated version info. 这将使用更新的版本信息更新npm-shrinkwrap文件。

See: https://docs.npmjs.com/cli/shrinkwrap 请参阅: https//docs.npmjs.com/cli/shrinkwrap

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM