从Git安装npm到特定版本 [英] npm install from Git in a specific version
问题描述
假设我已经为Node.js编写了一个模块,希望将其保密.我知道我可以(应该)添加以下行:
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"
到package.json
文件,我也知道我可以使用文件系统路径或git存储库(包括GitHub)的链接npm install
此模块.
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.
我也知道我可以在package.json
中放置这样的文件系统路径或git repo的链接,以便dependencies
部分看起来像这样:
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:..."
}
我现在想要的不是链接到最新版本,而是链接到特定版本.我知道的唯一可能性是使用其ID链接到特定的提交.但这比使用诸如0.3.1
这样的版本号的可读性和可维护性更差.
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
.
所以我的问题是:是否仍然可以指定这样的版本号,并使npm在git存储库中搜索包含该版本的最新提交?
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?
如果没有,您如何在项目中解决此问题?您是否拥有提交ID或对此有更好的解决方案?
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.
您当然可以创建并使用自己的注册表如果您的项目不应该公开共享,请使用 registry.npmjs.org
.
You can certainly create and use your own registry instead of registry.npmjs.org
if your projects shouldn't be shared publicly.
但是,如果不在注册表中,则必须由 URL 或 Git URL .要使用Git URL指定版本,请包含适当的 <commit-ish>
,例如作为标签,最后是 URL片段.
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.
示例,对于名为0.3.1
的标签:
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"
此外,当无法使用SSH访问时,将需要使用其他地址格式:
And, a different address format will be needed when SSH access isn't available:
"myprivatemodule": "git://github.com/{owner}/{project}.git#0.3.1"
根据您的操作系统,您还可以 link
到另一个文件夹中的依赖项它是从Github克隆的.
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屋!