DEV Community

Cover image for How to test your NPM package locally
Chris Bongers
Chris Bongers

Posted on • Originally published at daily-dev-tips.com

How to test your NPM package locally

We made our very first NPM package, and briefly touched on how to test it locally.

However, I felt this needs some more explanation.
As I hit this wall when making my NPM package.

You don't want to be that person pushing new versions just so you're able to test if something works.

And trust me, I did this 🤦‍♂️.

Link your NPM package locally

The first step is to open a terminal and navigate to your NPM package on your machine.

Now execute the following command in the terminal:

npm link
Enter fullscreen mode Exit fullscreen mode

This command will link this local package to your globally installed packages.

Import the link in a test project

On the other side, we need to link this package to the test project we want to try it out in.

Navigate to the test project and execute the following command.

npm link {package-name}
Enter fullscreen mode Exit fullscreen mode

I named my package npm-calculator, so I have to run:

npm link npm-calculator
Enter fullscreen mode Exit fullscreen mode

Now, if we look at our node_modules, we can see this is a symlink now!

Link a local NPM package

Now let's also actually try if it works. By creating a test index file, we can import our NPM package.

const {add, subtract, multiply} = require('npm-calculator');

console.log(add(1, 5));
console.log(subtract(10, 5));
console.log(multiply(2, 6));
Enter fullscreen mode Exit fullscreen mode

When we run the code now, it works. Our NPM package is loaded and worked locally.

How to test your NPM package locally

To try this out, edit your NPM package by, for instance, adding a console log. This is just for the sake of testing the local link.

I'll add a log in the add function.

Now without doing anything, head back to your test app and re-run the code.

Changed local NPM package

As you can see, our change works right away!
This makes for a much quicker development experience.
And again, once you're happy with this, you can go ahead and publish your NPM package to the registry.

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Top comments (14)

Collapse
 
iamandrewluca profile image
Andrei L • Edited

Some things to take into consideration. Let's supose that your npm-calculator also depends on react and your npm-calculator-test also depends on react
you will have react installed in 2 places

npm-calculator/node_modules/react
npm-calculator-test/node_modules/react
Enter fullscreen mode Exit fullscreen mode

Now when you use npm-calculator it will load react from here

npm-calculator/node_modules/react
Enter fullscreen mode Exit fullscreen mode

Instead from here

npm-calculator-test/node_modules/react
Enter fullscreen mode Exit fullscreen mode

because of how modules are resolved. And you may encounter strange errors.

To fix this you will need to tell your bundler to resolve all dependencies from npm-calculator-test/node_modules/** instead of npm-calculator/node_modules/**

webpack.js.org/configuration/resol...

webpack.config.js

module.exports = {
  resolve: {
    alias: {
      react: resolve(__dirname, 'node_modules', 'react'),
    },
  },
}
Enter fullscreen mode Exit fullscreen mode

And now every import of react should be loaded from here npm-calculator-test/node_modules/react

Collapse
 
thesecuritydev profile image
David

Just use PNPM

Collapse
 
jackmellis profile image
Jack

This isn't quite true. Npm is smart enough to resolve common versions of packages so if they both depended on the same major version of react, it would be fine.

This is probably a bad example, though, as 99.9% of the time react would be a peer dependency instead (which completely alleviates this problem and gives the version responsibility to the consumer)

Collapse
 
iamandrewluca profile image
Andrei L • Edited

I have to mention again, that this is a specific use case when you use npm link

Unfortunately that's true. NPM is just a package manager, install/uninstall dependencies. Node is the one who resolves dependencies.

nodejs.org/api/modules.html#module...

Smart enough should be the bundler to know what exactly to load.

github.com/iamandrewluca/example-n...

cd npm-calculator
npm install
cd ../npm-calculator-test
npm install
node index.js
Enter fullscreen mode Exit fullscreen mode
17.0.2
16.14.0
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
jackmellis profile image
Jack

I mentioned it in another comment but in my experience, npmjs.com/package/yalc solves all of these kind of issues. It's so useful

Collapse
 
dailydevtips1 profile image
Chris Bongers

Thanks Andrew! This is a useful addition actually!
I would have thought NPM would resolve only one of the dependencies based on it's version checks it does.

Collapse
 
iamandrewluca profile image
Andrei L • Edited

Created a small demo here github.com/iamandrewluca/example-n...

cd npm-calculator
npm install
cd ../npm-calculator-test
npm install
node index.js
Enter fullscreen mode Exit fullscreen mode

Outputs

17.0.2
16.14.0
Enter fullscreen mode Exit fullscreen mode
Collapse
 
yoursunny profile image
Junxiao Shi

npm link cannot catch errors in package creation, such as missing a necessary file in package.json "files" property.
I test my packages with npm pack as tarball, or by publishing to a private Verdaccio registry.

Collapse
 
dailydevtips1 profile image
Chris Bongers

Oh thanks for that one, wasn't aware of that option.
But definitely look into testing it like that as well ✨

Collapse
 
waylonwalker profile image
Waylon Walker

Packaging can be so freaking complicated. I have never built a node package, but this looks quite simple.

Collapse
 
dailydevtips1 profile image
Chris Bongers

It was my first public package so far the experience was quite good! :D

Collapse
 
jackmellis profile image
Jack

I use yalc for testing npm packages in development. Working in a team where we actively worked on 100+ packages this was a game changer!

Collapse
 
dailydevtips1 profile image
Chris Bongers

Oh nice thanks for that tip Jack, haven't heard of yalc, but will look into that as well.

Collapse
 
dailydevtips1 profile image
Chris Bongers

Awesome Chris,

Looking forward to seeing your package soon.