Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make npm or yarn install a specific version of a specific sub-package from github? [duplicate]

Tags:

git

github

npm

Is it possible to install npm package from github when the package located inside subfolder?

For example, we have Microsoft BotBuilder repository: https://github.com/Microsoft/BotBuilder

But I need to install package inside subfolder "Node/core/": https://github.com/Microsoft/BotBuilder/tree/master/Node/core/

So how can I install it with npm?

like image 830
Ceridan Avatar asked Aug 28 '16 18:08

Ceridan


People also ask

How do I install a specific version of a package?

Use npm list [package-name] to know the specific latest version of an installed package. Use npm install [package-name]@[version-number] to install an older version of a package. Prefix a version number with a caret (^) or a tilde (~) to specify to install the latest minor or patch version, respectively.

How do I update npm packages to a specific version?

To update a specific package, we need to run the npm update command followed by the package name. Sometimes, you want to update a package to the specific version in such cases you need to use npm install command by specifying a version number after the package name.

How do I install multiple versions of the same package in npm?

I found a solution by use a custom alias when installing a package with npm or yarn. Alias allows you to install multiple versions of a same package in the same project. When you want to install a specific version of the package append the command with @<package-version> .

How do I get a specific version of npm?

The command is npm install npm@latest -g to install it globally. This will install the latest version that will run with the node. js you have installed. Additionally you can install a specific version of npm to your package.


5 Answers

Add to package.json:

...
"scripts": {
  "postinstall": "mkdir BotBuilder; cd BotBuilder; git init; git remote add -f origin https://github.com/Microsoft/BotBuilder.git; git config core.sparseCheckout true; echo \"Node/core\" >> .git/info/sparse-checkout; git pull --depth=1 origin master; cd ..; npm i ./BotBuilder/Node/core/"
  ...
},
...

postinstall script is running after the package is installed.

And step by step:

  1. Make folder to clone repo: mkdir BotBuilder
  2. enter to the folder: cd BotBuilder
  3. init git repo: git init
  4. set git origin to Microsoft/BotBuilder repo: git remote add -f origin https://github.com/Microsoft/BotBuilder.git
  5. enable sparse checkout: git config core.sparseCheckout true
  6. add Node/core to checkout list: echo "Node/core" >> .git/info/sparse-checkout
  7. pull part of repo: git pull --depth=1 origin master
  8. enter to Your app folder: cd ..
  9. install BotBuilder: npm i ./BotBuilder/Node/core/
like image 138
Tomasz Jakub Rup Avatar answered Oct 04 '22 14:10

Tomasz Jakub Rup


Paste the github link to the subfolder into gitpkg. You can then use this along with yarn or npm to install the package from a github sub folder.

https://gitpkg.now.sh/

like image 31
MrBlenny Avatar answered Oct 04 '22 16:10

MrBlenny


If the package source is hosted on GitHub, you can use GitPkg like this:

# using npm:
npm install https://gitpkg.now.sh/<user>/<project>/<subdir>?<commit-ish>
# using yarn:
yarn add https://gitpkg.now.sh/<user>/<project>/<subdir>?<commit-ish>

For your particular case, the URL would be this:

https://gitpkg.now.sh/Microsoft/BotBuilder/Node/core?master

There is a nice wizard-like form at their site that helps build the URL, and the command to install it.

like image 41
Shayan Toqraee Avatar answered Oct 04 '22 14:10

Shayan Toqraee


Might be slightly off topic, just still relevant to the question

https://git-scm.com/book/en/v2/Git-Tools-Submodules

Git Submodules are git repos that you can use in other repos (henceforth, referred to as Supermodules). With each submodule having the usual assortment of branches features and tags, the benefit comes from each supermodule being a version controlled, pluggable components, that can be worked on separately or developed alongside the supermodule.

A Few Useful Commands

To add a submodule, you run the following inside your supermodule:

git submodule add <url-to-submodule-repo>

The submodule(s) still have to be initialized and fetched from the repo:

git submodule init git submodule update

A supermodule with submodules can be cloned and all submodules fetched by running:

git clone --recursive <url-to-supermodule>

You can pull upstream changes to a submodule's branch by running the following inside the submodule directory:

git fetch

Then run the following to update local code:

git merge

The following will fetch and merge for all submodules in your supermodule:

git submodule update --remote

If you want to track a specific branch of a submodule you can use the following:

git config -f .gitmodules submodule.<my-submodule>.branch fantastic_new_implementation

If you have worked on your supermodules and submodules and you push your supermodule, changes made to submodules will only exist locally and those you are collaborating with will not know of these changes. The following command will check if your submodules have been pushed BEFORE attempting to push your supermodule

git push --recurse-submodules=check

Finally, here is a useful ForEach command, that allows us to run a command for each submodule

git submodule foreach 'git checkout -b featureA

like image 37
T M Avatar answered Oct 04 '22 15:10

T M


Inspired by @Tomasz Jakub Rup's answer. I updated his example and pointed to the 3.0 branch which his example was based on and instead used the new-ish git feature called sparse-checkout. This feature will save time/bandwidth as it doesn't need to clone the entire repository ahead of time and will only grab what you directed. Many servers however don't support --filter option which does the bulk of the space saving, but --depth 1 in many cases will still cut down on bandwidth.

I used a .tmp_npm folder to minimize overwriting and also to possibly be .gitignored due to being a hidden file.

"scripts": {
  "postinstall": "mkdir .tmp_npm; cd .tmp_npm; git init; git clone --filter=blob:none --no-checkout --depth 1 --sparse -b 3.0 https://github.com/Microsoft/BotBuilder.git; cd BotBuilder/; git sparse-checkout init --cone; git sparse-checkout add Node/core; git checkout; cd ../..; npm i .tmp_npm/BotBuilder/Node/core/"
  ...
},
like image 35
Kerry Johnson Avatar answered Oct 04 '22 15:10

Kerry Johnson