This is part of a series of articles:
npm link
This is a very easy and straightforward solution. This npm command creates a symlink to a package folder.
To be able to use your own local libraries, you need to follow two simple steps:
-
First, run
npm link
from the root of your local package folder. This will create a symlink in the global folder{prefix}/lib/node_modules/<package>
that links to the package where the npm link command was executed. It will also link any bins in the package to{prefix}/bin/{name}
.{prefix}
is an npm variable that will depend on your operating system. To know the current value for your specific case, you can runnpm prefix –g
.Following the example of the first part of the series:
Let’s imagine we have a local package called
@ks/my-fancy-library
(defined in the name attribute of thepackage.json
file) in a folder called my-fancy-library.
$ npm prefix –g $ /Users/inigo/.nvm/versions/node/v16.13.1 $ cd my-fancy-library $ npm link
This will produce a symlink in the following location: -
Next, from the root folder of the project where you want to use your local package, just run
npm link <package-name>
. This will create a symbolic link from globally-installed package-name tonode_modules/
of the current folder.
$ cd my-awesome-project # root folder of the project where we want to use our package $ npm link @ks/my-fancy-library
This will produce a symlink in thenode_modules
folder.
Then, from our project my-awesome-project
, we will be able to import and use the local library @ks/my-fancy-library
.
🚨 Important
When running these commands, the current node version used is taken into account. If you want to test the library in different node environments, you need to repeat the process for each node version.
Top comments (0)