Npm link, wait, what!? Yeah npm link, a way to test npm packages as you develop them. This is a little something I learned last week and need to write it down so I remember how to do it.

I was working on the THAT Conference api and needed to add a custom GraphQL scalar for a ‘Slug’. In this particular case I am working with member slugs, though the Slug scalar will be utilized for any of our slug needs.

The package project, that-api, will contain the new Slug scalar located locally at ~/tc/that-api.

The api project, that-api-members, will be using the new scalar located locally at ~/tc/that-api-members.

After a bunch of hacking, etc. its was time to test the npm package in the api project. Here are the steps. This assumes that both packages pass tests, build and run. Both these projects use Babel and output to a folder named __build__. Package name is @thatconference/api

Starting with that-api

$ cd ~/tc/that-api
$ npm run validate
$ npm run build
$ cd __build__
$ npm link
$ cd ..
$ npm run dev

Now at the api project that-api-members

$ cd ~/tc/that-api-members
$ npm run validate
$ npm link @thatconference/api
$ npm run validate
$ npm run start:watch

At this point the that-api-members project is running using the linked that-api package, not the one downloaded into its node_modules folder.

So to undo this. I am not 100% sure this is correct, but it seems to work.

that-api. Begin by stopping the running code

$ ^c
$ cd ~/tc/that-api/__build__
$ npm unlink
$ cd ..

that-api-members. Again begin by stopping running code

$ ^c
$ cd ~/tc/that-api-members
$ npm unlink @thatconference/api
$ npm i @thatconference/api

When you unlink a package it removes it from packages.json, so we need to then add it back. This is why we removed the global link first so we could install and get the package, not the link.