[github] How to install an npm package from GitHub directly?

Trying to install modules from github results in:

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!     <[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

This question is related to github npm npm-install node-modules

The answer is


You could also do

npm i alex-cory/fasthacks

or

npm i github:alex-cory/fasthacks

Basically:

npm i user_or_org/repo_name

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

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.


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://[email protected]/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://[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

You can directly install an github repo by npm install command, like this: npm install https://github.com/futurechallenger/npm_git_install.git --save

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

  1. maybe you have to have a dist folder in you repo, according to @Dan Dascalescu's comment.
  2. You definitely have to have a package.json in you repo! which I forget add.

Try this command

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

this command worked for me.

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

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


If git is not installed, we can try

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

Simple :

npm install *GithubUrl*.git --save

example :

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

UPDATE now you can do: npm install git://github.com/foo/bar.git
or in package.json:

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

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

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

However, something notable that has changed recently is npm adding the prepare script to replace the prepublish script. This fixes a longstanding 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 https://github.com/npm/npm/issues/3055.

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


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.


You can also do npm install visionmedia/express to install from Github

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.


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.


Update September 2016

Installing from vanilla https github URLs now works:

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

EDIT 1: 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 repo is in an npm-usable state, you can now quite happily install directly from github

EDIT 2: (21-10-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).


Examples related to github

Does the target directory for a git clone have to match the repo name? Issue in installing php7.2-mcrypt How can I switch to another branch in git? How to draw checkbox or tick mark in GitHub Markdown table? How to add a new project to Github using VS Code git clone error: RPC failed; curl 56 OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054 How to add empty spaces into MD markdown readme on GitHub? key_load_public: invalid format git - remote add origin vs remote set-url origin Cloning specific branch

Examples related to npm

What does 'x packages are looking for funding' mean when running `npm install`? error: This is probably not a problem with npm. There is likely additional logging output above Module not found: Error: Can't resolve 'core-js/es6' Browserslist: caniuse-lite is outdated. Please run next command `npm update caniuse-lite browserslist` ERROR in The Angular Compiler requires TypeScript >=3.1.1 and <3.2.0 but 3.2.1 was found instead DeprecationWarning: Buffer() is deprecated due to security and usability issues when I move my script to another server Please run `npm cache clean` What exactly is the 'react-scripts start' command? On npm install: Unhandled rejection Error: EACCES: permission denied Difference between npx and npm?

Examples related to npm-install

How to update core-js to core-js@3 dependency? Can not find module “@angular-devkit/build-angular” How do I deal with installing peer dependencies in Angular CLI? NPM Install Error:Unexpected end of JSON input while parsing near '...nt-webpack-plugin":"0' How to solve npm install throwing fsevents warning on non-MAC OS? Why does "npm install" rewrite package-lock.json? What is the difference between npm install and npm run build? How to solve npm error "npm ERR! code ELIFECYCLE" base 64 encode and decode a string in angular (2+) Install specific branch from github using Npm

Examples related to node-modules

SyntaxError: Cannot use import statement outside a module What could cause an error related to npm not being able to find a file? No contents in my node_modules subfolder. Why is that? Node - was compiled against a different Node.js version using NODE_MODULE_VERSION 51 npm WARN enoent ENOENT: no such file or directory, open 'C:\Users\Nuwanst\package.json' Cannot find the '@angular/common/http' module How to use paths in tsconfig.json? Could not find a declaration file for module 'module-name'. '/path/to/module-name.js' implicitly has an 'any' type How to fix 'fs: re-evaluating native module sources is not supported' - graceful-fs How to provide a mysql database connection in single file in nodejs Git - Ignore node_modules folder everywhere