1199

Trying to install modules from GitHub results in this error:

ENOENT error on package.json.

Easily reproduced using express:

npm install https://github.com/visionmedia/express throws error.

npm install express works.

Why can't I install from GitHub?

Here is the console output:

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!     <npm-@googlegroups.com>

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
guy mograbi
  • 27,391
  • 16
  • 83
  • 122
  • 2
    Can anyone give the package.json devDependencies format? – Adam Sep 17 '16 at 18:47
  • 5
    @Adam you can add `"express": "github:visionmedia/express"` to the `"dependencies"` section of `package.json file`, then run: `npm install` (as mentioned below) – Danny Jan 05 '18 at 01:48
  • @danny, that doesn't work for me. i wonder if there was a dist folder in that express repo to enable it. – ml242 Jan 11 '18 at 17:14

22 Answers22

1527

Because https://github.com/visionmedia/express is the URL of a web page and not an npm module. Use this flavor: git+{url}.git

git+https://github.com/visionmedia/express.git

or this flavor if you need SSH:

git+ssh://git@github.com/visionmedia/express.git

or if you need to install a GitHub Enterprise repository:

git+https://<github enterprise url>/<org>/<repo>.git#<branch>
Manuel Spigolon
  • 11,003
  • 5
  • 50
  • 73
Peter Lyons
  • 142,938
  • 30
  • 279
  • 274
  • 105
    You can also use `git+https://github.com/visionmedia/express.git` to use https rather than ssh. – Steve Willcock Jul 07 '13 at 10:36
  • 58
    Note that repository you want to install must be a **npm module**, it must contain a `package.json` file or else you will get this error: `Error: ENOENT, open 'tmp.tgz-unpack/package.json'`. – GabLeRoux Jul 28 '14 at 14:18
  • 30
    what if I want a specific branch – kilianc Oct 22 '14 at 20:21
  • 5
    Only solution I think of for now is to specify a commit with .../express.git#commit – Gaston Sanchez Dec 08 '14 at 18:23
  • 64
    Worth saying that you might need to escape the `#` to use a specific branch from the shell, i.e.: `npm install git+https://github.com/user/repo.git\#branch` – mor Jul 07 '15 at 13:57
  • otherwise it uses master? – light24bulbs Aug 03 '15 at 20:37
  • I can't install anything whatsoever from publics repos using the URL structure. I've tried all the formats above. Any ideas? **Permission denied (publickey). npm ERR! fatal: Could not read from remote repository.** – SuperUberDuper Dec 12 '15 at 14:55
  • @SuperUberDuper troubleshoot your github ssh details according to their docs which include commands you can run to get more clues as to the problem: https://help.github.com/articles/error-permission-denied-publickey/ – Peter Lyons Dec 12 '15 at 17:20
  • 2
    @SuperUberDuper You apparently can't clone other people's repos using ssh unless your public key is added to the project. Use https instead. – Hubro Dec 15 '15 at 12:00
  • 2
    How do you require such a module after installing it... the module ends up in a heirarchy like this `@githubname/reponame`... and I've tried all sort of combinations which gives me `Error: Can't find module` – Ohenepee Peps Jun 21 '16 at 09:33
  • 1
    You require it just by its base name the same as if it were from npm: `const reponame = require('reponame')`. On disk the module does NOT end up in a directory matching the github username, they all just go in a flat list directly in `node_modules`. – Peter Lyons Jun 21 '16 at 13:52
  • Note: unlike the normal github clone-url (suggested in the browser ui), you do need the `git@` also for the https variant... thus → `npm install --save git+https://git@github.com:jakwuh/di-loader.git` – Frank N May 08 '17 at 08:52
  • 2
    @OhenepeePeps it may be that your npm package requires a build step (i.e. `npm run build`) in order to generate binaries/package files (or if not a build file that generates binaries, some other post-installation step to build artefacts not in the git source repository) it is likely that the requiring module cannot find those artefacts that are not installed/created from the original `npm install`. – lol Sep 17 '17 at 12:19
  • sadly this does not work with 'npm config proxy' configuration. So behind a proxy you are not able to install from github – Andre Sep 19 '17 at 09:08
  • I've used this with self-hosted Gitea as well. For me it only worked using the `git+ssh:` scheme and a tagged release slug following `.git#`. Repo must also have a package manifest. – vhs Feb 12 '19 at 13:58
  • How do you specify a specific version? – OZZIE Feb 22 '22 at 14:58
  • How do you do such a build step @lol? – Fr4nc3sc0NL Aug 05 '22 at 12:20
825

To install from GitHub, you can also do

npm install visionmedia/express

Or

npm install visionmedia/express#branch

There is also support for installing directly from a Gist, Bitbucket, GitLab, and a number of other specialized formats. Look at the npm install documentation for them all.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
JFSIII
  • 8,399
  • 1
  • 13
  • 6
219

If Git is not installed, we can try:

npm install --save https://github.com/Amitesh/gulp-rev-all/tarball/master
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Amitesh
  • 2,917
  • 2
  • 19
  • 14
  • 4
    You can also use tag names in place of `master`. Probably safer to do it that way. – mpen Jan 14 '16 at 19:30
  • Yes. It will be more safer. Thank you to bring this in focus. – Amitesh Jan 15 '16 at 04:27
  • 6
    Nice for when you are working with branches, you should modify your answer to include a general format answer, i didn't notice the `/tarball/` at first – Lu Roman Apr 11 '16 at 04:46
  • 2
    This is the only answer which worked for me when installing a dependency from within a Docker container. – Fela Maslen Apr 18 '19 at 13:06
  • 3
    This is also perfect for not requiring the entire repo to be downloaded. Thanks! – som Feb 04 '20 at 12:54
85

As of September 2016, installing from vanilla HTTPS GitHub URLs now works:

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

You can't do this for all modules because you are reading from a source control system, which may well contain invalid/uncompiled/buggy code. So to be clear (although it should go without saying): given that the code in the repository is in an npm-usable state, you can now quite happily install directly from GitHub.

In October 2019, we are now living through "peak TypeScript/React/Babel", and therefore JavaScript compilation has become quite common. If you need to take compilation into account, look into prepare. That said, NPM modules do not need to be compiled, and it is wise to assume that compilation is not the default, especially for older node modules (and possibly also for very new, bleeding-edge "ESNext"-y ones).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Fergie
  • 5,933
  • 7
  • 38
  • 42
  • @DanDascalescu could you be more specific? The relevance of that link is unclear in this context. – Fergie Oct 18 '16 at 10:30
  • 18
    You can't install any npm package you want from its GitHub source, unless the repo includes a `dist` folder, and most don't. The issue I linked to is an npm bug - the `prebuild` step is not run when installing from GitHub. As an example, try installing [node-influx/node-influx](https://github.com/node-influx/node-influx/issues/224). – Dan Dascalescu Oct 18 '16 at 18:31
  • 5
    Repos without `dist` folders CAN be installed from GitHub, for example: `npm install https://github.com/fergiemcdowall/search-index-adder` works perfectly. – Fergie Oct 19 '16 at 08:50
  • 8
    Right, because you have the directly usabel source in `/lib` (as if you had a dist folder). My point is that just including the GitHub URL of a repo in package.jons isn't guaranteed to work for installing that repo. Modules written in TypeScript, for example, need push their transpiled code into the repo. They typically don't do this, but rather use a prepublish script to dump the .JS code into a dist folder, which gets uploaded to npmjs.com. – Dan Dascalescu Oct 19 '16 at 10:30
  • All npm modules should have an entry-point that is a javascript file defined by `main` in package.json. So yes, if that is not set up correctly, then the module wont work as intended, but that is a separate issue, and I think we are getting into the weeds here. Or am I totally missing something? – Fergie Oct 19 '16 at 11:09
  • 3
    I know this is a bit late, but the trick is actually npm does not have a 1-to-1 mapping to Git repositories. Some projects build their source files before publishing rather than before committing, meaning they are NOT in the Git repo, but would be in the npm package - projects doing this will not work right from Git. Another example of how this can fail is macro repositories - Babel's GitHub project, for example, includes hundreds of individual npm packages in subfolders. They can be published individually because npm does not map directly to Git. npm publishes whatever is in your directory. – John Chadwick Jan 10 '17 at 19:03
  • Yes, of course: when installing directly from GitHub, the cloned module has to be in a useable state. – Fergie Jan 11 '17 at 11:10
  • Note that in NPM 5 this "little bit broken": it fixes your GitHub package commit hash, so now there are no difference between using a package published on NPM vs GitHub. We were using GitHub packages for *development* but @rebecca-turner seems to break that now :-( – Brian Cannard Jul 27 '17 at 18:18
  • 27
    For anyone who stumbles upon this later... As of npm5, npm will run any `prepare` script for bare "installs", which includes git deps. Which means the above comments around compilation and dist folders is out of date. Any package that properly sets their compilation to run on `prepare` will work just fine as a git dep without committing any compiled assets into git. – jasonkarns Nov 10 '17 at 05:37
  • Thats a good point @jasonkarns but it bears repeating that a node module doesn't have to be compiled- it can be plain old vanilla boring js, and structured in anyway the author sees fit SO LONG AS THE ENTRY POINT IS CORRECTLY DEFINED IN `main`. – Fergie Nov 14 '17 at 11:22
  • @Fergie A huge number of npm packages rely on a build step to produce code that is usable by a consuming app/library, so yes, the `prepare` script is a very important addition that allows equivalent behavior to the `prepublish` script for installs via git (as mentioned in my answer from July 2017). – nextgentech Jun 25 '19 at 07:00
  • @nextgentech Yes `prepare` is very nice. Compile modules that need to be compiled. Don't compile modules that don't need to be compiled. Avoid assumptions about the need for compilation. – Fergie Jun 25 '19 at 07:48
  • @DanDascalescu `prepare` is the correct way. If the repo is setup correctly it will generate missing `dist/` files. Just watch out when using `.gitignore` as it can *seem* broken. https://stackoverflow.com/questions/48287776/automatically-build-npm-module-on-install-from-github/57503862#57503862 – Cameron Tacklind May 12 '21 at 17:52
75

The methods are covered pretty well now in npm's install documentation as well as the numerous other answers here.

npm install git+ssh://git@github.com:<githubname>/<githubrepo.git[#<commit-ish>]
npm install git+ssh://git@github.com:<githubname>/<githubrepo.git>[#semver:^x.x]
npm install git+https://git@github.com/<githubname>/<githubrepo.git>
npm install git://github.com/<githubname>/<githubrepo.git>
npm install github:<githubname>/<githubrepo>[#<commit-ish>]

However, something notable that has changed recently is npm adding the prepare script to replace the prepublish script. This fixes a long-standing problem where modules installed via Git did not run the prepublish script and thus did not complete the build steps that occur when a module is published to the npm registry. See Run prepublish for Git URL packages #3055.

Of course, the module authors will need to update their package.json file to use the new prepare directive for this to start working.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nextgentech
  • 3,561
  • 1
  • 24
  • 19
  • 1
    Watch out for issues with `.gitignore` files! https://stackoverflow.com/questions/48287776/automatically-build-npm-module-on-install-from-github/57503862#57503862 – Cameron Tacklind May 12 '21 at 17:53
  • 1
    Note that the `git://` protocol is no longer supported (it was when this answer was written) – Taylor Nov 02 '21 at 19:17
  • This answer is excessive here and duplicates the previous answers. – Sergey Nevmerzhitsky Aug 19 '23 at 17:17
  • @SergeyNevmerzhitsky I wrote this answer 6 years ago and was the first to mention npm's prepare script addition in the context of installing directly from a github repo. Not sure what's excessive about including the basic install commands, links to various documentation, and very specific info about the prepare directive. – nextgentech Aug 22 '23 at 04:25
61

There's also npm install https://github.com/{USER}/{REPO}/tarball/{BRANCH} to use a different branch.

zakelfassi
  • 2,936
  • 1
  • 22
  • 20
  • I ran into this issue...https://github.com/yarnpkg/yarn/issues/2738 I was able to change the registry to the github tarball registry and it worked.. Thanks! – Matt Goo Jul 17 '17 at 21:51
  • This resolved some major performance issues for me. Using {USER}/{REPO}.git#{BRANCH} was very slow. – Erik Koopmans Oct 22 '17 at 12:35
  • THIS ^^^^ is what I'd been looking for. My library relies on a build step and didn't have a `prepare` script. I was just trying to install a branch of the whole repo in a test repo, but since the built code wasn't checked into GitHub, all that got installed was `package.json` and the readme. This `tarball` approach using the full URL installed all the source files, which is what I needed. – jdunning Sep 20 '20 at 23:14
  • @jdunning You do not need to check your compiled code into git to use the `prepare` method! You just need to watch out for issues from also including a `.gitignore`: https://stackoverflow.com/questions/48287776/automatically-build-npm-module-on-install-from-github/57503862#57503862 – Cameron Tacklind May 12 '21 at 17:58
49

The current top answer by Peter Lyons is not relevant with recent NPM versions. For example, using the same command that was criticized in this answer is now fine.

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

If you have continued problems it might be a problem with whatever package you were using.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Colin D
  • 2,822
  • 1
  • 31
  • 38
  • It also redirects to https://github.com/expressjs/express automatically now when you use this command :) – Colin D May 02 '16 at 16:10
29

The general form of the syntax is

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

which means for your case it will be

npm install git+ssh://git@github.com/visionmedia/express.git

From npmjs docs:

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.

<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:

npm install git+ssh://git@github.com:npm/npm.git#v1.0.27
npm install git+ssh://git@github.com:npm/npm#semver:^5.0
npm install git+https://isaacs@github.com/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://git@github.com:npm/npm.git npm install
ishandutta2007
  • 16,676
  • 16
  • 93
  • 129
28

This works for me and it is less typing.

npm i github:<UserName>/<RepoName>

package.json

{
    "dependencies": {
        "name": "github:<UserName>/<RepoName>"
    }
}
Omar Omeiri
  • 1,506
  • 1
  • 17
  • 33
26

You can do:

npm install git://github.com/foo/bar.git

Or in package.json:

"dependencies": {
  "bar": "git://github.com/foo/bar.git"
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sagiv Ofek
  • 25,190
  • 8
  • 60
  • 55
18

Install it directly:

npm install visionmedia/express

Alternatively, you can add "express": "github:visionmedia/express" to the "dependencies" section of package.json file, then run:

npm install
Tyler Liu
  • 19,552
  • 11
  • 100
  • 84
14

You could also do

npm i alex-cory/fasthacks

or

npm i github:alex-cory/fasthacks

Basically:

npm i user_or_org/repo_name
Alex Cory
  • 10,635
  • 10
  • 52
  • 62
12

Sometimes I need to install from a specific branch or commit. To make things simple I just use https://gitpkg.vercel.app/

enter image description here

sultanmyrza
  • 4,551
  • 1
  • 30
  • 24
10

You can directly install a GitHub repository by the npm install command, like this:

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

NOTE: In the repository which will be installed by npm command:

  1. maybe you have to have a dist folder in you repository, according to Dan Dascalescu's comment.
  2. You definitely have to have a package.json in your repository! Which I forget add.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bruce Lee
  • 4,177
  • 3
  • 28
  • 26
8

Simple:

npm install *GithubUrl*.git --save

Example:

npm install https://github.com/visionmedia/express.git --save
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Khurshid Ansari
  • 4,638
  • 2
  • 33
  • 52
3

I tried npm install git+https://github.com/visionmedia/express but that took way too long and I wasn't sure that would work.

What did work for me was - yarn add git+https://github.com/visionmedia/express.

Zephyr
  • 1,612
  • 2
  • 13
  • 37
3

Below piece of code worked for me to install from github repository:

npm install git+ssh://<your_repository_ssh_clone_link>#<branch_name_if_any>

You can get ssh clone link as below:

ssh clone screenshot

So for above screenshot repository, you may need to import as below,

npm install git+ssh://git@github.com:Siddhu2/calculator-chatbot.git#master

where master is my branch and it is optional since I have only one branch.

2

If you get something like this:

npm ERR! enoent undefined ls-remote -h -t https://github.com/some_repo/repo.git

Make sure you update to the latest npm and that you have permissions as well.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rip3rs
  • 1,284
  • 12
  • 21
2

The only command that worked for me was npm i MY_PACKAGE_NAME:MY_REPOSITORY#BRANCH_NAME

Toxnyc
  • 1,350
  • 7
  • 20
2

No need to do much; this helped me:

Yarn add <git_name>:<github_name>/<Repository_name>.git

Example:

yarn add git@github.com:myGitHub/dynamic-checkbox-input.git

And if you want to add some specific commit or branch name then add #.

Example:

yarn add git@github.com:myGitHub/dynamic-checkbox-input.git#master

Example:

yarn add git@github.com:myGitHub/dynamic-checkbox-input.git#c978U57
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
1

Yarn 2 requires the package name along with the Github repository. Read the documentation of Yarn's CLI.

Example: yarn add <package_name>git@github.com:<owner_name>/<package_name>.git

Guillem Puche
  • 1,199
  • 13
  • 16
-4

Try this command:

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

This command worked for me:

 npm install github:BlessCSS/bless#3.x -g
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rahil Lakhani
  • 412
  • 4
  • 7
  • The version is the tag / branch name or the package.json version ? – mfrachet Aug 25 '16 at 13:56
  • Why two commands? Should both be used (in that order). Or only one or the other? This is not clear. Can you elaborate? Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/38304770/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Jul 18 '21 at 21:55