clite: A Lightweight CLI Framework
clite is a lightweight command-line interface (CLI) framework for Node.js applications, designed to provide essential boilerplate functionality. Its current stable version is 0.3.0, last released in March 2016, indicating that the project is likely no longer actively maintained. The library focuses on automating common CLI tasks such as displaying version information from `package.json`, generating help documentation, handling `stdin` input, notifying users of updates via `update-notifier`, and managing exceptions with non-zero exit codes. It leverages `abbrev` for command/option aliasing and `yargs` for robust argument parsing, including booleans and options. Commands are promise-based and lazy-loaded to optimize boot times. A key differentiator is its minimal configuration approach, allowing developers to quickly scaffold CLI tools without extensive setup, though its age might pose compatibility challenges with modern Node.js environments.
Common errors
-
SyntaxError: Cannot use import statement outside a module
cause Attempting to use ES module syntax (`import`) with clite, which is a CommonJS-only package.fixChange `import clite from 'clite';` to `const clite = require('clite');` in your main CLI entry file. -
ReferenceError: Promise is not defined
cause Running clite on a very old Node.js version (pre-Node.js 4.0) where Promises were not natively supported, and the `es6-promise` polyfill included by clite is not effective or properly loaded.fixEnsure `es6-promise` is installed and that clite's internal polyfill is effective, or preferably, upgrade to Node.js v4.0 or higher where Promises are natively available. -
Error: Cannot find module './config'
cause The main clite script cannot locate the configuration file specified in the `require()` call.fixVerify that the path to your config file passed to `require()` in your main script (e.g., `clite(require('./config'));`) is correct and relative to the calling script's location.
Warnings
- breaking clite has not been updated since March 2016, making it potentially incompatible with modern Node.js versions (e.g., v16+, v18+, v20+) and newer JavaScript language features (ESM). Its internal dependencies may also be severely outdated.
- gotcha By default, clite catches all exceptions originating from command modules and exits the process with a non-zero code, printing the error message to stderr. This prevents higher-level application-specific error handling.
- gotcha clite relies on internal dependencies like `yargs` and `abbrev`. If these dependencies have had breaking changes in their latest versions that conflict with the specific versions clite expects (which are likely old), it could lead to unexpected behavior or runtime errors.
- deprecated The project uses CommonJS modules exclusively, which is considered a legacy module system in modern Node.js development. It does not support native ES Modules (`import`/`export` syntax) without transpilation or specific Node.js loader configurations.
Install
-
npm install clite -
yarn add clite -
pnpm add clite
Imports
- clite
import clite from 'clite';
const clite = require('clite'); - Configuration Object
clite({ commands: { /* ... */ } });clite(require('./config')); - Command Module
export default function myCommand(args) { /* ... */ };module.exports = function myCommand(args) { /* ... */ };
Quickstart
/* cli.js */
#!/usr/bin/env node
const clite = require('clite');
const path = require('path');
// Load configuration for the CLI application
clite(require('./config'));
/* config.js */
module.exports = {
commands: {
'_': path.join(__dirname, 'commands', 'default'), // Default command if no argument provided
'greet': path.join(__dirname, 'commands', 'greet'),
'delay': path.join(__dirname, 'commands', 'delay')
},
booleans: ['verbose', 'help'],
options: ['name', 'message'],
alias: {
'v': 'verbose',
'n': 'name'
},
help: path.join(__dirname, 'help', 'main.txt') // Path to a main help file
};
/* commands/default.js */
module.exports = function defaultCommand(args) {
if (args.help) {
return `Usage: my-cli [command] [--name <name>] [--message <message>] [--verbose]
Default command echoes arguments or shows a welcome message.
Try 'my-cli greet --name World' to greet someone.`;
}
if (args._.length > 0) {
return `You ran the default command with arguments: ${args._.join(' ')}`;
}
return 'Welcome to the clite demo! Try `my-cli greet --name John`.';
};
/* commands/greet.js */
module.exports = function greetCommand(args) {
const name = args.name || 'Stranger';
const message = args.message || 'Hello';
if (args.verbose) {
return `Preparing to greet ${name} with message: "${message}"`;
}
return `${message}, ${name}!`;
};
/* commands/delay.js */
module.exports = function delayCommand(args) {
const duration = parseInt(args._[0] || '1000', 10); // First argument is delay duration
return new Promise(resolve => {
setTimeout(() => resolve(`Delayed for ${duration}ms. All done!`), duration);
});
};
/* help/main.txt */
// This is a simple help file for the 'my-cli' tool.
// Use 'my-cli --help' for general help.
// Use 'my-cli greet --help' for command specific help (if implemented).