How to test your NPM package locally

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

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 🤦♂️.
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
This command will link this local package to your globally installed packages.
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}
I named my package npm-calculator, so I have to run:
npm link npm-calculator
Now, if we look at our node_modules, we can see this is a symlink now!

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));
When we run the code now, it works. Our NPM package is loaded and worked 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.

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 my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter