Create a consumer for your library

July 2017
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.

Step 3: Link your library

Now comes the magic. 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 {HelloWorld} from 'hwrld'
HelloWorld.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 ...

Interested in full stack TypeScript?