Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
December 22, 2022 03:25 pm GMT

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:

  1. 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 run npm 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 the package.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:

    Symlink of the library in the global npm folder

  2. 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 to node_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 the node_modules folder.

Symlink of the library in the project's node_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

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To