npm 网络管理员指南_节点和Npm版本编号:指南和最佳实践

本文是npm网络管理员指南,探讨了在创建Node项目时如何选择和管理Node及其依赖的版本。内容涵盖了特定版本、版本范围以及最佳实践,帮助理解语义版本控制在Node模块声明中的应用。
摘要由CSDN通过智能技术生成

npm 网络管理员指南

When creating a new Node project, one of the very first things you'll do is select your dependencies and devDependencies for your project. When declaring Node dependency version numbers, there are many different ways to specify the version number you want. That's because Node uses semantic versioning when declaring modules.

Let's say we're bringing

创建新的Node项目时,要做的第一件事就是为项目选择dependenciesdevDependencies 。 声明节点依赖版本号时,有许多不同的方法来指定所需的版本号。 这是因为Node在声明模块时使用语义版本控制

假设我们正在将 expressJS into our project. You can declare that dependency version many ways. Here are a few examples. expressJS引入我们的项目。 您可以通过多种方式声明该依赖项版本。 这里有一些例子。
  • 4.8.5

    4.8.5
  • >4.8.5

    >4.8.5
  • >=4.8.5

    >=4.8.5
  • ~4.8.5

    ~4.8.5
{
  "name": "awesome-sauce",
  "main": "server.js",
  "dependencies": {
    "express": "^4.0.x" <-- what in the world is that?!
  }
}
Let's look through the various ways we can declare version numbers in our Node package.json files. package.json文件中声明版本号的各种方法。

特定版本 ( A Specific Version )

v2.0.0 or v2.0.0=2.0.0 The =2.0.0 v and the v= will be removed and the exact version =将被删除,并将使用确切的版本 2.0.0 will be used. 2.0.0

Version Ranges

There are a lot of ways to define a version range. Why would we need to define a version range? There are many reasons why you would want version ranges. When modules and dependencies that you use are updated, you want to make sure that they don't break your project. This is why it's good to not specify latest as your version. For example, when ExpressJS was updated from version 3 to version 4, many applications would have broken. It would have been good to specify that you only wanted the latest of version 3, but not anything from version 4. Here's a handy table of the ways we can define version ranges:
Version Number Explanation
latest Takes the latest version possible. Not the safest thing to use.
*, x Wildcards. Can be any version at all. Crazy stuff.
4, 4.*, 4.x, ~4, ^4 Any version that starts with 4. Takes the latest.
>4.8.5 Choose any version greater than a specific version. Could break your application.
<4.8.5 Choose any version lower than a specific version. >=4.8.5 Anything greater than or equal to a specific version. <=4.8.5 Anything less than or equal to. 4.8.3 - 4.8.5 Anything within a range of versions. The equivalent of >=4.8.3 and <=4.8.5
~4.8.5 Any version "reasonably close to 4.8.5". This will call use all versions up to, but less than 4.9.0
~4.8 Any version that starts with 4.8
^4.8.5 Any version "compatible with 4.8.5". This will call versions up to the next major version like 5.0.0. Could break your application if there are major differences in the next major version.
~1.2 Any version compatible with 1.2

版本范围

有很多定义版本范围的方法。 为什么我们需要定义版本范围? 有许多原因导致您需要版本范围。 当您使用的模块和依赖项更新时,您要确保它们不会破坏您的项目。 这就是为什么最好不要将 latest版本指定为您的版本。 例如,当ExpressJS从 版本3更新 到版本4时 ,许多应用程序将损坏。 最好指定您只需要最新的版本3,而不需要版本4的任何内容。这是我们定义版本范围的便捷表格:
版本号 说明
latest 尽可能采用最新版本。 不是最安全的使用方法。
*x 通配符。 可以是任何版本。 疯狂的事情。
4 . 4.*4.x~4^4 以4开头的任何版本。
>4.8.5 选择大于特定版本的任何版本。 可能会中断您的应用程序。
<4.8.5 选择低于特定版本的任何版本。 >=4.8.5 大于或等于特定版本的任何内容。 <=4.8.5 任何小于或等于。 4.8.3 - 4.8.5 版本范围内的任何内容。 等于> = 4.8.3和<= 4.8.5
~4.8.5 任何版本“合理接近4.8.5”。 这将调用使用所有但不超过4.9.0的版本
~4.8 4.8开头的任何版本
^4.8.5 任何版本“与4.8.5兼容”。 这将调用高达下一个主要版本(如5.0.0)的版本。 如果下一个主要版本中有主要差异,则可能会中断您的应用程序。
~1.2 任何与1.2兼容的版本

Best Practices

Personally, when calling dependencies for my project, I will use the tilde ( ~). By specifying your express dependency using ~4.8.5, you will be able to get bug fixes when new smaller versions come around, but you won't grab versions that break your project. You are never going to be sure what updates come with the new versions and if those updates will break your application. For express, we definitely won't want to grab version 5 in the future. Even 4.9.0 could break our application so the tilde will keep us safe since it won't let our application grab that version. TLDR; Use the tilde so you don't break your applications, but still get the latest bug fixes.

最佳实践

就个人而言,当为我的项目调用依赖项时,我将使用波浪号( ~ )。 通过使用 ~4.8.5指定明确的依赖关系,当出现新的较小版本时,您将能够修复错误,但是 您不会抓到破坏项目的版本 。 您永远不会确定新版本附带哪些更新以及这些更新是否会破坏您的应用程序。 为了明确起见,我们绝对不会在将来获得版本5。 甚至4.9.0都可能破坏我们的应用程序,因此波浪号将使我们安全,因为它不会让我们的应用程序获取该版本。 TLDR; 使用波浪号,这样您就不会破坏应用程序,但仍然可以获取最新的错误修复

结论 ( Conclusion )

希望这可以澄清所有这些疯狂的符号和版本编号方案的含义。 语义版本控制不仅仅可以在npm模块中看到。 您可以看到它在Grunt和Bower版本中也使用过。

翻译自: https://scotch.io/tutorials/node-and-npm-version-numbering-guide-and-best-practices

npm 网络管理员指南

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值