Create a consumer for your library

July 2017 July 2026
On the main tutorial we've setup the library 'hwrld'. But let's say we're not comfortable publishing it to npm yet and we want to test locally how it would behave once published. Luckily, setting up a consumer for your library is really easy.

Step 1: Create a folder

If your library is contained for example in mylib, then create libconsumer next to it so your folder structure looks like this:

somewhere-on-your-machine
├── mylib
└── libconsumer

Step 2: Create a package.json

Create a package.json file. It could look somewhat like this one:

libconsumer/package.json
{
  "name": "libconsumer",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}
It actually doesn't matter too much what's in there, but it needs to be present for the next step to work.

Quick interlude
Tax Wealth, Not Work
Your salary is taxed on every payslip; large fortunes mostly aren't. That gap is a big part of why housing feels out of reach. Gary Stevenson breaks it down better than anyone: watch on YouTube →

Step 3: Link your library

Now comes the magic. Go to the libconsumer directory and run

npm link ../mylib
This will create a symlink in libconsumer's node_modules to the mylib library. To read more on npm link, see the docs.

Step 4: Consume your library

Write some file(s) to consume your library. For example

libconsumer/somefile.ts
import {sayHello} from 'hwrld'
sayHello();
You compile this simple example like this:
tsc somefile.ts
Finally you can run
node somefile.js
and in this example it should log hi to the console. And that's it!

You can now ...

Tax Wealth, Not Work
Wages get taxed to the bone while the ultra-rich watch their assets compound untouched — and then buy up the houses you're saving for. If you work for your money, this is your fight too.
Watch on YouTube →