Yalc

Yalc is a package authoring tool, allowing us to create packages from JavaScript libraries locally. This is useful during package authoring and for testing as well as just plain, allowing us to create our own local packages and not need to have them distributed to some remote registry.

Installing yalc

To install yalc, execute the following command

yarn global add yalc

// or using npm

npm i yalc -g

Creating a package locally

First off we need a package.json file, so the simplest way is to run

yan init --yes

this will create a default package.json file which you should edit, so for example removing main and adding the private key, which will ensure the package cannot accidentally get published. For example

{
  "name": "MyPackage",
  "version": "1.0.0",
  "license": "MIT",
  "private": true
}

Running the following command will create a package based upon the package.json and files within the same folder as the package.json. As we’ve marked the package.json as private we’ll need to use the –private switch to force the package creation

yalc publish --private

The package will be stored in the folder listed below on Windows, obviously replacing the {username} with your user name and {packagename} with your package name.

C:\Users\{username}\AppData\Local\Yalc\packages\{packagename}

Using the newly created package

We’ll obviously now want to use our local package, so within your application run, again replacing the {packagename} with your newly created package name.

yalc add {packagename}

This command will add the package to your package.json in your application with a file: prefix. Obviously your package will also be added to your node_modules folder.

I’m not quite sure why, but I also found that this was a very useful technique for some code which seemed to have module problems when I tried to add the code to my application. Once packaged, the module issues went away.