依赖使用 package.json 中的 git URL 的分支或标签?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16350673/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 16:04:50  来源:igfitidea点击:

Depend on a branch or tag using a git URL in a package.json?

gitdependenciesnpmbranchgit-tag

提问by hurrymaplelad

Say I've forked a node module with a bugfix and I want to use my fixed version, on a feature branch of course, until the bugfix is merged and released.

假设我已经分叉了一个带有错误修复的节点模块,并且我想在功能分支上使用我的固定版本,直到错误修复被合并和发布。

How would I reference my fixed version in the dependenciesof my package.json?

我将如何在dependencies我的package.json.

回答by hurrymaplelad

From the npm docs:

npm 文档

git://github.com/<user>/<project>.git#<branch>

git://github.com/<user>/<project>.git#feature\/<branch>

As of NPM version 1.1.65, you can do this:

从 NPM 版本 1.1.65 开始,您可以执行以下操作:

<user>/<project>#<branch>

回答by justingordon

per @dantheta's comment:

根据@dantheta 的评论:

As of npm 1.1.65, Github URL can be more concise user/project. npmjs.org/doc/files/package.json.html You can attach the branch like user/project#branch

从 npm 1.1.65 开始,Github URL 可以更简洁的用户/项目。npmjs.org/doc/files/package.json.html 你可以像 user/project#branch 这样附加分支

So

所以

"babel-eslint": "babel/babel-eslint",

Or for tag v1.12.0 on jscs:

或者对于 jscs 上的标签 v1.12.0:

"jscs": "jscs-dev/node-jscs#v1.12.0",

Note, if you use npm --save, you'll get the longer git

注意,如果你使用 npm --save,你会得到更长的 git

From https://docs.npmjs.com/files/package.json#git-urls-as-dependencies

来自https://docs.npmjs.com/files/package.json#git-urls-as-dependencies

Git URLs as Dependencies

Git urls can be of the form:

git://github.com/user/project.git#commit-ish git+ssh://user@hostname:project.git#commit-ish git+ssh://user@hostname/project.git#commit-ish git+http://user@hostname/project/blah.git#commit-ishgit+https://user@hostname/project/blah.git#commit-ish

The commit-ish can be any tag, sha, or branch which can be supplied as an argument to git checkout. The default is master.

GitHub URLs

As of version 1.1.65, you can refer to GitHub urls as just "foo": "user/foo-project". Just as with git URLs, a commit-ish suffix can be included. For example:

{ "name": "foo",
"version": "0.0.0",
"dependencies": { "express": "visionmedia/express", "mocha": "visionmedia/mocha#4727d357ea" } }

Git URL 作为依赖项

Git 网址可以采用以下形式:

git://github.com/user/project.git#commit-ish git+ssh://user@hostname:project.git#commit-ish git+ssh://user@hostname/project.git#commit- ish git+ http://user@hostname/project/blah.git#commit-ishgit+ https://user@hostname/project/blah.git#commit-ish

commit-ish 可以是任何可以作为参数提供给 git checkout 的标签、sha 或分支。默认是主。

GitHub 网址

从 1.1.65 版本开始,您可以将 GitHub 网址称为“foo”:“user/foo-project”。就像 git URL 一样,可以包含一个 commit-ish 后缀。例如:

{ "name": "foo",
"version": "0.0.0",
"dependencies": { "express": "visionmedia/express", "mocha": "visionmedia/mocha#4727d357ea" } }

回答by Fizer Khan

If you want to use develor featurebranch, or you haven't published a certain package to the NPM registry, or you can't because it's a private module, then you can point to a git://URI instead of a version number in your package.json:

如果你想使用develfeature分支,或者你还没有发布某个包到 NPM 注册中心,或者你不能因为它是一个私有模块,那么你可以git://在你的URI 中指向一个URI 而不是版本号package.json

"dependencies": {
   "public": "git://github.com/user/repo.git#ref",
   "private": "git+ssh://[email protected]:user/repo.git#ref"
}

The #refportion is optional, and it can be a branch (like master), tag (like 0.0.1) or a partial or full commit id.

#ref部分是可选的,它可以是分支(如master)、标签(如0.0.1)或部分或完整提交 ID。

回答by vortex

On latest version of NPM you can just do:

在最新版本的 NPM 上,您可以执行以下操作:

npm install gitAuthor/gitRepo#tag

npm install gitAuthor/gitRepo#tag

If the repo is a valid NPM package it will be auto-aliased in package.json as:

如果 repo 是一个有效的 NPM 包,它将在 package.json 中自动别名为:

{ "NPMPackageName": "gitAuthor/gitRepo#tag" }

{ "NPMPackageName": "gitAuthor/gitRepo#tag" }

If you could add this to @justingordon 's answer there is no need for manual aliasing now !

如果您可以将其添加到 @justingordon 的答案中,那么现在就不需要手动别名了!

回答by MattS

If it helps anyone, I tried everything above (https w/token mode) - and still nothing was working. I got no errors, but nothing would be installed in node_modules or package_lock.json. If I changed the token or any letter in the repo name or user name, etc. - I'd get an error. So I knew I had the right token and repo name.

如果它对任何人有帮助,我尝试了上面的所有内容(https w/token 模式) - 但仍然没有任何效果。我没有遇到任何错误,但在 node_modules 或 package_lock.json 中不会安装任何东西。如果我更改了存储库名称或用户名等中的令牌或任何字母 - 我会收到错误消息。所以我知道我有正确的代币和回购名称。

I finally realized it's because the name of the dependency I had in my package.json didn't match the name in the package.json of the repo I was trying to pull. Even npm install --verbose doesn't say there's any problem. It just seems to ignore the dependency w/o error.

我终于意识到这是因为我在 package.json 中的依赖名称与我试图提取的 repo 的 package.json 中的名称不匹配。甚至 npm install --verbose 也没有说有任何问题。它似乎只是忽略了没有错误的依赖项。