npm

Overview

The npm object provides access to all npm packages.

Using packages

To use a package, simply get it by name from npm's properties.

sample Use the dedupe package
const friendNames = await cli.ask('What are your friends called?', Array);
const uniqueFriendNames = npm.dedupe(friendNames);

cli.tell('Total count of unique friend names: ' + uniqueFriendNames.length);

You do not need to import or install the packages you use in any way; Tasklemon will automatically install and inject any package you use.

To access a package with special characters in its name, use bracket notation.

sample Use the UUID package from Allthings
const uuid = npm['@allthings/uuid'];
cli.tell('New unique identifier: ' + uuid());

Requiring specific versions

If your script needs a specific version of a package, you can add a package version directive at the top of your script.
Add the directive below the shebang, if any, and above the script's code.

sample Specify which version of username to use
// tl:require: username@5.0.0

cli.tell('Hello, ' + await npm.username() + '!');

It's a good idea to do this, to ensure your script keeps the same behavior over time, even after new versions of the packages are released. To have Tasklemon automatically add a tl:require directive for all the packages used by your script, setting them to their latest available version, you can use the --pin-pkg command-line action.

sample Automatically add version directives to a script
$ lemon --pin-pkg script.js

Requiring other files

If your script needs to use a specific sub-file of a package, rather than the package's main file, you can specify it by using a colon to separate the package name from the sub-file path.

sample Use the uuid package
const uuid = npm['uuid:v4'];
cli.tell('New unique identifier: ' + uuid());

Managing the cache

In order to ensure a script can execute offline, and without any download delays, you can ask Tasklemon to preload all of the script's required packages. These include packages you implicitely require, not just the packages pinned by version directives.
This has no purpose for a script that's been run at least once, since Tasklemon will already have downloaded its packages.

sample Preload the packages used by a script
$ lemon --preload-pkg script.js

Tasklemon's package cache is generally very resilient, and can recover from most download and installation issues. If something seems broken, however, you can try emptying the package cache.

sample Empty the package cache
$ lemon --clear-pkg-cache