npm link: What the first `npm link` really does and how it can also be used
Published 2018-11-28, 13:39
There are loads of articles about npm link
, how it works (npm link
in library dir, then npm link library
in project that should use that local library as a dependency) and what it can be used for (linking to a local folder for a dependency instead of having a npm downloaded version in node_modules
).
But most skip over or only scrape what the first of the two steps really does – and how that can be used besides the default use case.
TLDR: npm link
not only creates a symlink to the library in the global node_modules
(that can be used in the second step of npm link ...
), but also makes the library main
script or binary accessible via a global npm module, similar to what happens when you npm install -g
something.
The details:
When you run npm link
as the first step of the two-step linking process, you create a „symlink in the global folder„.
Let’s follow the individual sub steps:
C:\Projects\Cordova
λ npm list -g --depth=0
C:\Program Files\nodejs
+-- npm@6.4.1
We start in a specific folder (C:\Projects\Cordova
in my example) and check our globally installed packages: We have nothing besides npm
installed on this machine.
C:\Projects\Cordova
λ cd cordova-paramedic
C:\Projects\Cordova\cordova-paramedic
λ npm link
removed 1 package in 3.649s
C:\Program Files\nodejs\cordova-paramedic -> C:\Program Files\nodejs\node_modules\cordova-paramedic\main.js
C:\Program Files\nodejs\node_modules\cordova-paramedic -> C:\Projects\Cordova\cordova-paramedic
Now we switch the directory of our library that we want to link, cordova-paramedic
here. We run npm link
which creates two symlinks:
C:\Program Files\nodejs\cordova-paramedic -> C:\Program Files\nodejs\node_modules\cordova-paramedic\main.js
C:\Program Files\nodejs\node_modules\cordova-paramedic -> C:\Projects\Cordova\cordova-paramedic
The first one is a symlink for the „binary“, the main
script, of our library.
The second is a symlink for the whole library into the global node_modules
.
(C:\Program Files\nodejs\
is our global node/npm folder on this machine)
That first symlink is not mentioned in most of the articles about npm link
on the web as the example libraries being linked don’t include a main
script in their package.json
.
λ npm list -g --depth=0
C:\Program Files\nodejs
+-- cordova-paramedic@0.6.0-dev -> C:\Projects\Cordova\cordova-paramedic
`-- npm@6.4.1
When we check the globally installed modules again, we see that our library was added to that list.
So not only can we now use npm link cordova-paramedic
, the second command in the npm link two step process, to add this library to any project from our local checkout – but we can also use it as a global command cordova-paramedic
!
C:\Projects\Cordova
λ cordova-paramedic
Error missing args.
...
So using npm link
you can not only link local development versions of libraries to use in another project, but you can also create a global command that uses a local version of your command code. This is very useful when developing such a library.
Zum Artikel npm link: What the first `npm link` really does and how it can also be used sind noch keine Kommentare vorhanden. Deine Meinung wäre jedoch willkommen!