Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for GitHub URLs in package.json
Michal Bryxí
Michal Bryxí

Posted on • Edited on

     

GitHub URLs in package.json

package.json file in yournode project contain (among others) links to the packages that your projects needs to run.

To add new package dependency, you can runnpm add command:

 npm add chalk
Enter fullscreen modeExit fullscreen mode

And after that respective package will be added to thedependencies section of yourpackage.json:

{"name":"sandbox","main":"index.js","dependencies":{"chalk":"^4.1.2"}}
Enter fullscreen modeExit fullscreen mode

But what if instead of apublished npm package I want to use a branch that is living on GitHub? Luckilypackage.json definition allows GitHub URLs. The syntax is one of:

github:[user]/[project]github:[user]/[project]#[commit-sha]github:[user]/[project]#[branch]github:[user]/[project]#[tag]
Enter fullscreen modeExit fullscreen mode

Thegithub: part is optional, but gives some context to your future self.

Whereuser andproject you can extract from the GitHub URLs of respective repositories:https://github.com/[user]/[project]/

So for example to install commit95d74cbe8d3df3674dec1445a4608d3288d8b73c of the package/projectchalk (which happens to have GitHubuser of the same name) I would have to specify:

{"name":"sandbox","main":"index.js","dependencies":{"chalk":"github:chalk/chalk#95d74cbe8d3df3674dec1445a4608d3288d8b73c"}}
Enter fullscreen modeExit fullscreen mode

Semver

There is one more curious format that can be used in GitHub URLs and that issemver. The syntax is:

github:[user]/[project]#semver:[semver]"
Enter fullscreen modeExit fullscreen mode

So for example to installchalk withsemver^2.4.1 we should put following into ourpackage.json:

{"name":"sandbox","main":"index.js","dependencies":{"chalk":"github:chalk/chalk#semver:^2.4.1"}}
Enter fullscreen modeExit fullscreen mode

Photo byJJ Ying onUnsplash

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
lulalaby profile image
Lala Sabathil
Hey there! I'm Lala, 25 years old and do stuff on discord
  • Email
  • Location
    Germany
  • Pronouns
    she/her
  • Work
    Library Developer for @Discord
  • Joined

You have a typo here 🤣
Image description

CollapseExpand
 
michalbryxi profile image
Michal Bryxí
Cycle 🚴 , climb 🗻 , run 🏃 , travel 🌍 , enjoy life ♥.IT guy with the need to live fully.
  • Email
  • Location
    Interlaken, Switzerland
  • Education
    University of West Bohemia
  • Pronouns
    he/him
  • Work
    Sandcastle Architect
  • Joined

Ha! Actually took me a while to spot thegithug. Thanks for pointing that out!

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Cycle 🚴 , climb 🗻 , run 🏃 , travel 🌍 , enjoy life ♥.IT guy with the need to live fully.
  • Location
    Interlaken, Switzerland
  • Education
    University of West Bohemia
  • Pronouns
    he/him
  • Work
    Sandcastle Architect
  • Joined

More fromMichal Bryxí

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp