如何直接从GitHub安装npm包?

How to install an npm package from GitHub directly?

尝试从github安装模块导致:

ENOENT error on package.json.

使用快递轻松复制:

npm install https://github.com/visionmedia/express抛出错误。

npm install express有效。

为什么我不能从github安装?

这是控制台输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
npm http GET https://github.com/visionmedia/express.git
npm http 200 https://github.com/visionmedia/express.git
npm ERR! not a package /home/guym/tmp/npm-32312/1373176518024-0.6586997057311237/tmp.tgz
npm ERR! Error: ENOENT, open '/home/guym/tmp/npm-32312/1373176518024-0.6586997057311237/package/package.json'
npm ERR! If you need help, you may report this log at:
npm ERR!     <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR!     <[email protected]>

npm ERR! System Linux 3.8.0-23-generic
npm ERR! command"/usr/bin/node""/usr/bin/npm""install""https://github.com/visionmedia/express.git"
npm ERR! cwd /home/guym/dev_env/projects_GIT/proj/somename
npm ERR! node -v v0.10.10
npm ERR! npm -v 1.2.25
npm ERR! path /home/guym/tmp/npm-32312/1373176518024-0.6586997057311237/package/package.json
npm ERR! code ENOENT
npm ERR! errno 34
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR!     /home/guym/dev_env/projects_GIT/proj/somename/npm-debug.log
npm ERR! not ok code 0


因为https://github.com/visionmedia/express是网页的URL而不是npm模块。使用这种味道:

1
git+https://[email protected]/visionmedia/express.git

如果你需要SSH,还是这种味道:

1
git+ssh://[email protected]/visionmedia/express.git


您也可以从Github安装npm install visionmedia/express

要么

1
npm install visionmedia/express#branch

还支持直接从Gist,Bitbucket,Gitlab和许多其他专用格式进行安装。查看所有的npm install文档。


如果没有安装git,我们可以试试

1
npm install --save https://github.com/Amitesh/gulp-rev-all/tarball/master


还有npm install https://github.com/{USER}/{REPO}/tarball/{BRANCH}来使用不同的分支。


2016年9月更新

从vanilla https github URL安装现在可以正常工作:

1
npm install https://github.com/fergiemcdowall/search-index.git

编辑:有几个用户评论说你不能为所有模块执行此操作,因为您正在从源控制系统中读取,这可能包含无效/未编译/错误的代码。所以要清楚(虽然它应该不言而喻):鉴于repo中的代码处于npm可用状态,你现在可以非常高兴地直接从github安装


Peter Lyons目前的最佳答案与最近的NPM版本无关。例如,使用在这个答案中被批评的相同命令现在很好。

1
$ npm install https://github.com/visionmedia/express

如果您继续遇到问题,那么您使用的任何软件包都可能存在问题。


这些方法现在在npm的安装文档以及其他许多答案中都有很好的介绍。

1
2
3
4
5
npm install git+ssh://[email protected]:<githubname>/<githubrepo.git[#<commit-ish>]
npm install git+ssh://[email protected]:<githubname>/<githubrepo.git>[#semver:^x.x]
npm install git+https://[email protected]/<githubname>/<githubrepo.git>
npm install git://github.com/<githubname>/<githubrepo.git>
npm install github:<githubname>/<githubrepo>[#<commit-ish>]

但是,最近发生变化的值得注意的是npm添加prepare脚本来替换prepublish脚本。这解决了一个长期存在的问题,即通过git安装的模块没有运行prepublish脚本,因此没有完成将模块发布到npm注册表时发生的构建步骤。请参阅https://github.com/npm/npm/issues/3055。

当然,模块作者需要更新他们的package.json以使用新的prepare指令开始工作。


现在更新您可以:npm install git://github.com/foo/bar.git
或者在package.json中:

1
2
3
"dependencies": {
 "bar":"git://github.com/foo/bar.git"
}

语法的一般形式是

1
<protocol>://[<user>[:<password>]@]<hostname>[:<port>][:][/]<path>[#<commit-ish> | #semver:<semver>]

这意味着你的情况

1
npm install git+ssh://[email protected]/visionmedia/express.git

来自npmjs文档:

npm install :

Installs the package from the hosted git provider, cloning it with
git. For a full git remote url, only that URL will be attempted.

1
<protocol>://[<user>[:<password>]@]<hostname>[:<port>][:][/]<path>[#<commit-ish>

| #semver:] is one of git, git+ssh, git+http,
git+https, or git+file.

If # is provided, it will be used to clone exactly that
commit. If the commit-ish has the format #semver:,
can be any valid semver range or exact version, and npm will look for
any tags or refs matching that range in the remote repository, much as
it would for a registry dependency. If neither # or

semver: is specified, then master is used.

If the repository makes use of submodules, those submodules will be
cloned as well.

If the package being installed contains a prepare script, its
dependencies and devDependencies will be installed, and the prepare
script will be run, before the package is packaged and installed.

The following git environment variables are recognized by npm and will
be added to the environment when running git:

  • GIT_ASKPASS
  • GIT_EXEC_PATH
  • GIT_PROXY_COMMAND
  • GIT_SSH
  • GIT_SSH_COMMAND
  • GIT_SSL_CAINFO GIT_SSL_NO_VERIFY

See the git man page for details.

Examples:

1
2
3
4
5
npm install git+ssh://[email protected]:npm/npm.git#v1.0.27
npm install git+ssh://[email protected]:npm/npm#semver:^5.0
npm install git+https://[email protected]/npm/npm.git
npm install git://github.com/npm/npm.git#v1.0.27
GIT_SSH_COMMAND='ssh -i ~/.ssh/custom_ident' npm install git+ssh://[email protected]:npm/npm.git npm install

直接安装:

1
npm install visionmedia/express

或者,您可以将"express":"github:visionmedia/express"添加到package.json文件的"dependencies"部分,然后运行:

1
npm install

你也可以这样做

1
npm i alex-cory/fasthacks

要么

1
npm i github:alex-cory/fasthacks

基本上:

1
npm i user_or_org/repo_name


您可以通过npm install命令直接安装github repo,如下所示:

npm install https://github.com/futurechallenger/npm_git_install.git --save

注意:在将由npm命令安装的repo中:

  • 根据@Dan Dascalescu的评论,也许你必须在你的回购中有一个dist文件夹。
  • 你肯定必须在你的回购中有一个package.json!我忘了添加。

  • 简单:

    1
    npm install *GithubUrl*.git --save

    例如:

    1
    npm install https://github.com/visionmedia/express.git --save

    试试这个命令

    1
     npm install github:[Organisation]/[Repository]#[master/BranchName] -g

    这个命令对我有用。

    1
     npm install github:BlessCSS/bless#3.x -g