An Interest In:
Web News this Week
- April 2, 2024
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
Different approaches to testing your own packages locally: npm link
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, lets 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-awesome-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.
Original Link: https://dev.to/one-beyond/different-approaches-to-testing-your-own-packages-locally-npm-link-4hoj
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To