72

Loooking at the npm install docs it looks possible to npm install from a github repo.

Is it also possible to install specifically from a pull request?

Is the solution just to install based on the last commit (last sha) of the pull request?

sfletche
  • 47,248
  • 30
  • 103
  • 119

2 Answers2

109

GitHub is maintaining a namespace for each PR in the original repo, so this works as well:

npm install <user>/<repo>#pull/<id>/head

NOTE: It doesn't seeem to be working with NPM v. 5. See the comment below. Works with npm 7.0.23 and possibly earlier versions. See the comment below.

for example:

npm i --save-dev json-schema-faker/json-schema-faker#pull/129/head

or with yarn:

yarn add <user>/<repo>#<id>/head

for example:

yarn add json-schema-faker/json-schema-faker#129/head

Note that in Yarn case there is no pull/ segment in the package identifier.

This may be helpful if you need to automate the installation or repo / branch from where PR is originating is removed. See also Modifying an inactive pull request locally at GitHub.

rofrol
  • 14,438
  • 7
  • 79
  • 77
Tad Lispy
  • 2,806
  • 3
  • 30
  • 31
  • This works, but: How do I install multiple pull requests (and merge them) ? – Munchkin Oct 13 '16 at 08:25
  • 2
    @Munchkin I don't think it's something you would do with NPM itself. Maybe fork the repository, merge the PR's in your fork and install from there? – Tad Lispy Oct 13 '16 at 08:28
  • 2
    Yarn (or Github anymore?) doesn't seem to support pull in the tag. So for the example above it would be: json-schema-faker/json-schema-faker#129/head. There is also a 'merge' (rather than 'head') suffix for some PRs but I don't know what that means. – Damon Maria Mar 24 '17 at 04:09
  • @DamonMaria It seems to be specific to yarn. Original `npm` client v. 4.1.2 still works with the command I've provided. I'll edit the answer for `yarn`. Thank you for pointing that out. – Tad Lispy Mar 24 '17 at 12:11
  • Here I've specifically asked [a question about the difference between `npm` and `yarn` clients](http://stackoverflow.com/questions/42999809/why-is-the-reference-for-a-package-to-be-installed-from-a-pull-request-different) – Tad Lispy Mar 24 '17 at 12:44
  • @TadeuszŁazurski Does this still work? I have tried to install a PR for next-routes, and i noticed that it does pull the repo but doesn't seem to run it, only the src directory is in the folder tree. where as when i add the repo without the PR it has a dist directory... Is there a step i'm missing? https://github.com/fridays/next-routes/pull/37 yarn add fridays/next-routes#pull/37/head – shaune May 20 '17 at 09:30
  • 1
    @shaune Yes, it's a known limitation of npm clients. They do not compile packages installed from git repositories. Take a look at my [npm-git-install](https://www.npmjs.com/package/npm-git-install) project for more details and possible solution. – Tad Lispy May 20 '17 at 09:59
  • 3
    I get the message that the 'pull/242/head' did not match any file(s) known to git – AlxVallejo Jul 31 '17 at 22:30
  • 1
    Hello, @AlxVallejo. Indeed it's not working in NPM v. 5. Unfortunately I can't investigate it right now. What I would do is to analyze the output of `npm install --verbose ` for versions `4.x.x` (where it's working) and `5.x.x`. If you are using [NVM](https://github.com/creationix/nvm) it's easy to switch: `nvm install 7` for NPM 4 and `nvm install 8` for NPM 5. Good luck and please share your findings. – Tad Lispy Aug 01 '17 at 09:19
  • @TadLispy I tried this and it updated the package.json correctly, but when I compile the code I get an error : Module not found: Error: Can't resolve [module name]. Any idea? – S.R Dec 02 '20 at 18:20
  • @S.R Not sure. Sorry. I think it's best if you ask a question (instead of a comment) with the contents of you `package.json` and error logs. Then maybe link it in the comment. Good luck! – Tad Lispy Dec 02 '20 at 18:29
  • @TadLispy I posted this question: https://stackoverflow.com/questions/65121548/npm-install-for-a-specific-pull-request . Thanks! – S.R Dec 03 '20 at 07:30
  • works with npm 7.0.15. Don't know about previous versions. – rofrol Dec 16 '20 at 14:24
  • Worth noting that this won't build the PRed repo for you, only get you the source files, you'll still need a `npm run build` or `yarn build` in your build script. – martinedwards Mar 04 '21 at 14:58
  • not wokring to me: `npm i mrousavy/react-native-vision-camera#pull/1198/head` – famfamfam Jun 09 '23 at 03:11
30

"How to install NodeJS package from GitHub directly?" mentions that you can specify a branch.

So if you know the repo and branch from which a PR comes from, you can do a:

npm install git+https://github.com/user/repo.git#branch 

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 

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'. 
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • If you get the following error, you might not have git installed correctly (or at all - like in an Alpine image) ``` npm ERR! code ENOENT npm ERR! syscall spawn git npm ERR! path git npm ERR! errno -2 npm ERR! enoent An unknown git error occurred ``` – MountainAsh Jul 27 '22 at 18:25
  • @MountainAsh Good point. Having Git installed is a prerequisite. – VonC Jul 27 '22 at 18:30