DEV Community

Íñigo Marquínez for One Beyond

Posted on • Updated on

Different approaches to testing your own packages locally: npm link

This is part of a series of articles:

  1. Linking local files
  2. npm link
  3. npm yalc
  4. Verdaccio
  5. Relative deps

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:

    Projects structure

    Let’s 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-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)