如何在发布非 Node-API 版本的同时发布 Node-API 版本的包
【How to publish a Node-API version of a package alongside a non-Node-API version】
以下步骤使用 iotivity-node 包进行说明:
【The following steps are illustrated using the package iotivity-node:】
- 首先,发布非 Node-API 版本:
- 更新
package.json中的版本号。对于iotivity-node,版本号变为1.2.0-2。 - 完成发布清单(确保测试/演示/文档无误)
npm publish
- 更新
- 然后,发布 Node-API 版本:
- 更新
package.json中的版本号。对于iotivity-node,版本号变为1.2.0-3。关于版本控制,我们建议遵循 semver.org 描述的预发布版本方案,例如1.2.0-napi。 - 完成发布清单(确保测试/演示/文档无误)
npm publish --tag n-api
- 更新
在这个例子中,通过给发布版本打上 n-api 标签,确保了尽管版本 1.2.0-3 比非 Node-API 发布的版本(1.2.0-2)更新,但如果有人选择通过简单运行 npm install iotivity-node 来安装 iotivity-node,它仍不会被安装。默认情况下,这将安装非 Node-API 版本。用户需要运行 npm install iotivity-node@n-api 才能获取 Node-API 版本。有关使用 npm 标签的更多信息,请查看 使用 dist-tags。
【In this example, tagging the release with n-api has ensured that, although
version 1.2.0-3 is later than the non-Node-API published version (1.2.0-2), it
will not be installed if someone chooses to install iotivity-node by simply
running npm install iotivity-node. This will install the non-Node-API version
by default. The user will have to run npm install iotivity-node@n-api to
receive the Node-API version. For more information on using tags with npm check
out "Using dist-tags".】
如何引入一个基于 Node-API 的包依赖
【How to introduce a dependency on a Node-API version of a package】
要将 iotivity-node 的 Node-API 版本添加为依赖,package.json 将如下所示:
【To add the Node-API version of iotivity-node as a dependency, the package.json
will look like this:】
"dependencies": {
"iotivity-node": "n-api"
}
如在["使用 dist-tags"][]中解释的那样,与常规版本不同,被标记的版本不能在
package.json中通过类似"^2.0.0"的版本范围来引用。原因是标签只对应确切的某一个版本。因此,如果包的维护者选择用相同的标签标记该包的较新版本,npm update将会获取该较新版本。如果希望接受非最新发布的版本,package.json的依赖必须引用确切的版本,如下所示:
"dependencies": {
"iotivity-node": "1.2.0-3"
}