node.js 在特定版本中从 Git 安装 npm
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14187956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
npm install from Git in a specific version
提问by Golo Roden
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:
假设我已经为 Node.js 编写了一个我想保密的模块。我知道我可以(应该)添加以下行:
"private": "true"
to the package.jsonfile, and I also know that I can npm installthis module using a file system path or a link to a git repository, including GitHub.
到package.json文件,我也知道我可以npm install使用文件系统路径或指向 git 存储库(包括 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 dependenciespart may look somewhat like this:
我也知道我可以将这样的文件系统路径或 git repo 的链接放入package.json,以便该dependencies部分看起来有点像这样:
"dependencies": {
"myprivatemodule": "[email protected]:..."
}
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.
我现在想要的不是链接到最新版本,而是链接到特定版本。我所知道的唯一可能性是使用其 ID 链接到特定提交。但这比使用诸如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?
所以我的问题是:是否可以指定这样的版本号并让 npm 在 git 存储库中搜索包含此版本的最新提交?
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?
如果没有,你如何在你的项目中解决这个问题?您是否使用提交 ID,或者是否有更好的解决方案?
回答by surjikal
The accepted answer did not work for me. Here's what I'm doing to pull a package from github:
接受的答案对我不起作用。这是我从 github 中提取包的操作:
npm install --save "git://github.com/username/package.git#commit"
Or adding it manually on package.json:
或者在 package.json 上手动添加:
"dependencies": {
"package": "git://github.com/username/package.git#commit"
}
回答by Jonathan Lonowski
A dependencyhas to be available from the registryto be installed just by specifying a versiondescriptor.
一个依赖关系必须是可以从registry要由刚安装指定一个version描述符。
You can certainly create and use your own registryinstead of registry.npmjs.orgif your projects shouldn't be shared publicly.
你当然可以创建和使用自己的注册表,而不是registry.npmjs.org如果你的项目,不应该被公开分享。
But, if it's not in a registry, it'll have to be referenced by URLor 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.
但是,如果它不在注册表中,则必须通过URL或Git URL引用。要使用 Git URL 指定版本<commit-ish>,请在URL 片段的末尾包含适当的,例如标记。
Example, for a tag named 0.3.1:
例如,对于名为 的标签0.3.1:
"dependencies": {
"myprivatemodule": "[email protected]:...#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": "[email protected]:{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"
注意:上面的代码片段显示的基本 URL 与问题中发布的相同。
剪下的部分 (
...) 应填写:"myprivatemodule": "[email protected]:{owner}/{project}.git#0.3.1"而且,当 SSH 访问不可用时,将需要不同的地址格式:
"myprivatemodule": "git://github.com/{owner}/{project}.git#0.3.1"
Depending on your OS, you may also be able to linkto the dependency in another folder where you have it cloned from Github.
根据您的操作系统,您可能还可以link在另一个文件夹中找到从 Github 克隆的依赖项。
回答by qubyte
If by version you mean a tag or a release, then github provides download links for those. For example, if I want to install fetchversion 0.3.2 (it is not available on npm), then I add to my package.jsonunder dependencies:
如果版本是指标签或发布,那么 github 会提供下载链接。例如,如果我想安装fetch版本 0.3.2(它在 npm 上不可用),那么我添加到我的package.json下dependencies:
"fetch": "https://github.com/github/fetch/archive/v0.3.2.tar.gz",
The only disadvantage when compared with the commit hash approach is that a hash is guaranteed not to represent changed code, whereas a tag could be replaced. Thankfully this rarely happens.
与提交散列方法相比,唯一的缺点是散列保证不代表更改的代码,而标签可以被替换。幸运的是,这种情况很少发生。
Update:
更新:
These days the approach I use is the compact notation for a GitHub served dependency:
这些天我使用的方法是 GitHub 服务依赖项的紧凑符号:
"dependencies": {
"package": "github:username/package#commit"
}
Where commit can be anything commitish, like a tag. In the case of GitHub you can even drop the initial github:since it's the default.
提交可以是任何提交,比如标签。在 GitHub 的情况下,您甚至可以删除初始值,github:因为它是默认值。
回答by bvj
My example comment to @qubyte abovegot chopped, so here's something that's easier to read...
我的例子@qubyte评论上面得到切碎的,所以这里的东西是更易于阅读...
The method @surjikal described aboveworks for branch commits, but it didn't work for a treecommit I was trying include.
上面描述的方法@surjikal适用于分支提交,但它不适用于我尝试包含的树提交。
The archive mode also works for commits. For example, fetch@ a2fbf83
归档模式也适用于提交。例如获取@a2fbf83
npm:
纳米:
npm install https://github.com/github/fetch/archive/a2fbf834773b8dc20eef83bb53d081863d3fc87f.tar.gz
yarn:
纱线:
yarn add https://github.com/github/fetch/archive/a2fbf834773b8dc20eef83bb53d081863d3fc87f.tar.gz
format:
格式:
https://github.com/<owner>/<repo>/archive/<commit-id>.tar.gz
这是 tree需要
/archive//archive/模式的树提交:yarn add https://github.com/vuejs/vuex/archive/c3626f779b8ea902789dd1c4417cb7d7ef09b557.tar.gz
for the related vuex commit
对于相关的vuex 提交
回答by Prisacari Dmitrii
This command installs npm package username/packagefrom specific git commit:
此命令username/package从特定的 git commit安装 npm 包:
npm install https://github.com/username/package#3d0a21cc
Here 3d0a21ccis first 8 characters of commit hash.
这3d0a21cc是提交哈希的前 8 个字符。
回答by andrew
I describe here a problem that I faced when run npm install- the package does not appear in node_modules.
我在这里描述了我在运行时遇到的一个问题npm install- 包没有出现在node_modules.
The issue was that the namevalue in package.jsonof installed package was different than the name of imported package (key in package.jsonof my project).
问题是已安装包的name值package.json与导入包的名称不同(package.json我项目的输入)。
So if your installed project name is some-package(name value in its package.json) then
in package.jsonof your project write: "some-package": "owner/some-repo#tag".
因此,如果您安装的项目名称是some-package(名称中的值package.json),那么在package.json您的项目中写入:"some-package": "owner/some-repo#tag".
回答by duhaime
I needed to run two versions of tfjs-coreand found that both needed to be built after being installed.
我需要运行两个版本的tfjs-core,发现都需要在安装后构建。
package.json:
包.json:
"dependencies": {
"tfjs-core-0.14.3": "git://github.com/tensorflow/tfjs-core#bb0a830b3bda1461327f083ceb3f889117209db2",
"tfjs-core-1.1.0": "git://github.com/tensorflow/tfjs-core#220660ed8b9a252f9d0847a4f4e3c76ba5188669"
}
Then:
然后:
cd node_modules/tfjs-core-0.14.3 && yarn install && yarn build-npm && cd ../../
cd node_modules/tfjs-core-1.1.0 && yarn install && yarn build-npm && cd ../../
And finally, to use the libraries:
最后,使用库:
import * as tf0143 from '../node_modules/tfjs-core-0.14.3/dist/tf-core.min.js';
import * as tf110 from '../node_modules/tfjs-core-1.1.0/dist/tf-core.min.js';
This worked great but is most certainly #hoodrat
这很有效,但肯定是#hoodrat
回答by Igor Soarez
If you're doing this with more than one module and want to have more control over versions, you should look into having your own private npm registry.
如果您使用多个模块执行此操作并希望对版本有更多控制,您应该考虑拥有自己的私有 npm 注册表。
This way you can npm publish your modules to your private npm registry and use package.json entries the same way you would for public modules.
通过这种方式,您可以将模块 npm 发布到您的私有 npm 注册表,并以与公共模块相同的方式使用 package.json 条目。

