Directly installing a package with npm install
and referring to its local path is a quick way to work with a local package. To be safe though, the usage of npm link
is still recommended.
The npm link
way
To work with local NPM packages on can reside to using npm link
. You make a package available locally with npm link
and later on symlink it into your project using npm link package
:
cd ~/projects/node-redis # go into the package directory
npm link # creates global link
cd ~/projects/node-bloggy # go into some project directory
npm link redis # link-install the redis package (by its name)
This methods works fine and – above all – allows you to have a package.json
that has no record of that link. That way you can check it into Git and push it, while still using the local version.
A downside however is that this way of working change is not explicit: by checking your package.json
you can’t really tell whether you’re using a locally linked version or not.
~
The relative path way
A more easy quick and dirty way of achieving this is to directly install a package by simply referring to its directory:
cd ~/projects/node-bloggy # go into some project directory
npm install ../node-redis # install the redis package by referring to its directory
Apart from symlinking the package into your project, your package.json
will have been adjusted like this:
{
…
"dependencies": {
"redis": "file:../node-redis"
}
}
This change is explicit: a look at your package.json
clearly tells you that you’re using a locally installed package.
⚠️ Do note that this way of working comes with a big caveat though: you may not commit this change into Git. While this change Works Fine on my Machine™ it won’t in your build system, your colleague their machine, …
~
So which technique to use when? Personally I go for:
npm link
when adjusting an existing library (most likely a clone/fork to land a bugfix) while testing it in an existing projectnpm install ../path
when splitting off a new library from an existing project
~
Thank me with a coffee.
I don\'t do this for profit but a small one-time donation would surely put a smile on my face. Thanks!
To stay in the loop you can follow @bramus or follow @bramusblog on Twitter.