DEV Community

Jesus Sabroso for One Beyond

Posted on • Updated on

Different approaches to testing your own packages: local files

Introduction

Let’s say you’re developing a new package; it doesn’t matter if it’s a really cool library of React components or a great new set of utils for NodeJS based projects. We all know that the first step to making sure that it will be compatible with different node versions and will work smoothly and without errors is having a proper set of tests.

However, perhaps you also want to test it locally in one of your projects before publishing it to npm / yarn so everyone can start using it. And thinking wider, not only focus on the first release but also take into account new updates that you want to try before publishing them.

In this series of posts, we’ll describe four alternatives you can use to achieve this purpose. They are sorted by difficulty, but that’s not the only factor to take into account, because the most advanced ones also offer a set of utils that the easier ones don’t have. So, it’s up to you to decide which one fits best for your use case.

We’ll briefly explain how to use these techniques and the benefits of:

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

So, let’s start with the first approach!

Local files

There is an extremely easy way to add a package to our project from a local dependency, and those are the steps.

First of all, let’s imagine we have our projects structured as in the following image

Folder filesystem with two projects at same level

  • Go to the root project of your library, install the package and build the library of the local package:
$ cd my-fancy-library 
$ npm i && npm run build 
Enter fullscreen mode Exit fullscreen mode
  • As the next step we must move the project to where we need to use our fancy library as a local package and install it
$ cd my-awesome-project 
$ npm i –-save ../my-fancy-library 
Enter fullscreen mode Exit fullscreen mode

This command will create a link between the two projects, adding the library as dependency

package.lock entry with relative route to a my fancy library folder

And create a link inside the node_modules/@ks folder

my fancy library folder with sym link

  • Now every time we modify the local package, we must rebuild the library to allow our project to detect the changes and rebuild it, or.... we can update the package.json file and add a new command to watch and rebuild the library on every change: "build:watch": "rollup -c --watch && copyfiles -f ./src/**/assets/* ./dist/assets"

Top comments (0)