The npm
object provides access to all npm packages.
Both ESM and CommonJS exports are supported.
You do not need to import or install the packages you use in any way; Tasklemon will automatically download and inject any package you access.
Modern npm packages usually expose their functionality as ESM modules.
To use such a package, access it through the npm
global:
npm.chalk
returns the default export of the chalk
package.
npm.telegram.TelegramClient
returns the TelegramClient
export of the telegram
package.
cli.tell(npm.boxen(" Hello! "));
/* ┌────────┐
Displays: │ Hello! │
└────────┘ */
cli.tell(npm.stringz.length("👩🏿💻 Contributing"));
// Displays “14”
In rare cases, there can be collisions between injected named exports, and default export properties. See Accessing shadowed default export properties for a solution.
Many other packages, including older ones, expose their functionality as CommonJS modules.
You can use these packages in the same way as ESM-based packages, by accessing them directly on the npm
global.
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);
If a package has conflicting ESM and CommonJS exports, Tasklemon will return the ESM export.
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.
// tl:require: username@5.0.0
cli.tell('Hello, ' + await npm.username() + '!');
It's a good idea to do this, to ensure your script's behavior stays the same 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.
$ lemon --pin-pkg script.js
To access a package with special characters in its name, such as a scoped package, use bracket notation.
const uuid = npm['@allthings/uuid'];
cli.tell('New unique identifier: ' + uuid());
If your script needs to use a specific sub-file of a package, rather than the package's main file, you can specify the file's path, separated by a colon.
const uuid = npm['uuid:v4'];
cli.tell('New unique identifier: ' + uuid());
Most of the time, when using ESM packages, there is no collision between named exports, and properties on the default export.
However, if you do need to access a property that's been shadowed, you can do so through the raw default export, accessible as the unmodifiedDefaultExport
property.
For example, consider a package named contrivedExample
. Its default export has an ambiguousLabel
property; and one of its named exports happens to be named ambiguousLabel
as well. In this situation:
npm.contrivedExample.ambiguousLabel
returns the ambiguousLabel
named export.npm.contrivedExample.unmodifiedDefaultExport.ambiguousLabel
returns the ambiguousLabel
property of the default export.
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.
$ 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.
$ lemon --clear-pkg-cache