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

时间:2022-08-24 07:26:07

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后手动删除^字符,这有点无聊。

2 个解决方案

#1


5  

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. It is located here:

新首选项存储在基于用户的自定义npm配置文件中。它位于这里:

~/.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

#2


0  

I'd suggest using 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。它将创建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. Or, to upgrade a single package dependency, run npm install --save <package-name>. This will update the npm-shrinkwrap file with that updated version info.

然后,当您想要升级所有依赖项时,删除npm-shrinkwrap.json文件,运行npm install并重新运行npm shrinkwrap。或者,要升级单个包依赖项,请运行npm install --save 。这将使用更新的版本信息更新npm-shrinkwrap文件。

See: https://docs.npmjs.com/cli/shrinkwrap

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

#1


5  

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. It is located here:

新首选项存储在基于用户的自定义npm配置文件中。它位于这里:

~/.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

#2


0  

I'd suggest using 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。它将创建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. Or, to upgrade a single package dependency, run npm install --save <package-name>. This will update the npm-shrinkwrap file with that updated version info.

然后,当您想要升级所有依赖项时,删除npm-shrinkwrap.json文件,运行npm install并重新运行npm shrinkwrap。或者,要升级单个包依赖项,请运行npm install --save 。这将使用更新的版本信息更新npm-shrinkwrap文件。

See: https://docs.npmjs.com/cli/shrinkwrap

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