在特定版本中从 Git 安装 npm [英] npm install from Git in a specific version

查看:29
本文介绍了在特定版本中从 Git 安装 npm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我已经为 Node.js 编写了一个模块,我希望将其保密.我知道我可以(应该)添加这一行:

"private": "true"

package.json文件,我也知道我可以npm install这个模块使用文件系统路径或git存储库的链接,包括GitHub.

我也知道我可以把这样一个文件系统路径或一个git repo的链接放到package.json中,这样dependencies部分可能看起来有点像这个:

依赖项":{"myprivatemodule": "git@github.com:..."}

我现在想要的不是链接到最新版本,而是链接到特定版本.我所知道的唯一可能性是使用其 ID 链接到特定提交.但这比使用诸如 0.3.1 之类的版本号可读性更差,可维护性更差.

所以我的问题是:是否可以指定这样的版本号并让 npm 在 git 存储库中搜索包含此版本的最新提交?

如果没有,您如何在您的项目中解决这个问题?您是否接受提交 ID,或者是否有更好的解决方案?

解决方案

依赖必须是可以从 registry 获得,只需通过 指定version 描述符.

您当然可以创建和使用您自己的注册表如果您的项目不应该公开共享,则使用 registry.npmjs.org 代替.

但是,如果它不在注册表中,则必须由 URL 引用Git URL.要使用 Git URL 指定版本,请包含适当的 ,比如标签,最后是网址片段.

例如,对于名为 0.3.1 的标签:

依赖项":{"myprivatemodule": "git@github.com:...#0.3.1"}

<块引用>

注意:以上代码段显示的基本 URL 与问题中发布的相同.

剪下的部分(...)应填写:

"myprivatemodule": "git@github.com:{owner}/{project}.git#0.3.1"

而且,当 SSH 访问不可用时,将需要不同的地址格式:

"myprivatemodule": "git://github.com/{owner}/{project}.git#0.3.1"

<小时>

根据您的操作系统,您还可以link 到从 Github 克隆的另一个文件夹.

Assumed that I have written a module for Node.js which I would like to keep private. I know that I can (should) add the line:

"private": "true"

to the package.json file, and I also know that I can npm install this module using a file system path or a link to a git repository, including GitHub.

I also know that I can put such a file system path or a link to a git repo into package.json, so that the dependencies part may look somewhat like this:

"dependencies": {
  "myprivatemodule": "git@github.com:..."
}

What I now want is not to link to the latest version, but to a specific one. The only possibility I know of is to link to a specific commit using its ID. But this is way less readable and worse maintainable than using a version number such as 0.3.1.

So my question is: Is it possible to specify such a version number anyway and make npm search the git repository for the latest commit that includes this version?

If not, how do you resolve this issue in your projects? Do you live with commit IDs or is there a better solution to this?

解决方案

A dependency has to be available from the registry to be installed just by specifying a version descriptor.

You can certainly create and use your own registry instead of registry.npmjs.org if your projects shouldn't be shared publicly.

But, if it's not in a registry, it'll have to be referenced by URL or Git URL. To specify a version with a Git URL, include an appropriate <commit-ish>, such as a tag, at the end as a URL fragment.

Example, for a tag named 0.3.1:

"dependencies": {
  "myprivatemodule": "git@github.com:...#0.3.1"
}

Note: The above snippet shows the base URL the same as it was posted in the question.

The snipped portion (...) should be filled in:

"myprivatemodule": "git@github.com:{owner}/{project}.git#0.3.1"

And, a different address format will be needed when SSH access isn't available:

"myprivatemodule": "git://github.com/{owner}/{project}.git#0.3.1"


Depending on your OS, you may also be able to link to the dependency in another folder where you have it cloned from Github.

这篇关于在特定版本中从 Git 安装 npm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆