Vorpal CLI Framework
Vorpal is a framework designed for building interactive command-line interface (CLI) applications in Node.js, positioning itself as an early pioneer in creating immersive CLI environments. It offers a comprehensive API for defining commands, handling arguments and options, enabling piped commands, providing persistent history, built-in help, and tab-completion. The framework is built upon and inspired by `commander.js` for its command structure and `inquirer.js` for interactive prompting, creating an isolated, shell-like experience. The current stable version, 1.12.0, was released some time ago, and the project is explicitly in an 'OPEN Open Source' state, actively seeking new maintainers, indicating a minimal or inactive release cadence from the original author. Its key differentiator is providing a full interactive shell within Node.js, facilitating complex, rich CLI applications through a simple API and extensibility.
Common errors
-
require is not defined in ES module scope
cause Attempting to use `require('vorpal')` in a file that Node.js interprets as an ES Module (e.g., a `.mjs` file or when `type: 'module'` is set in `package.json`).fixRename your file to `.cjs` or set `type: 'commonjs'` in your `package.json` for that file/directory. Alternatively, if your setup supports it, consider a dynamic import: `const vorpal = (await import('vorpal')).default();` (though direct instantiation might differ). -
TypeError: vorpal is not a function
cause Invoking `require('vorpal')` directly without the `()` to instantiate the Vorpal object.fixEnsure you call the required module as a function: `const vorpal = require('vorpal')();` -
The CLI prompt does not return or hangs after command execution.
cause The `callback()` function passed to the command's `action` method was not invoked.fixEnsure `callback()` is called at the end of your command's `action` function, signaling completion to Vorpal.
Warnings
- breaking The Vorpal project is currently in 'OPEN Open Source' status, with the original author unable to actively maintain it and seeking new volunteers. This means bug fixes, security patches, and new features are highly unlikely unless new maintainers step forward.
- breaking Vorpal's declared `engines` (`node >= 0.10.0`, `iojs >= 1.0.0`) are for extremely old Node.js versions. Running Vorpal on modern Node.js environments (e.g., Node.js 16+ or 18+) may lead to compatibility issues, deprecated API warnings, or runtime errors due to breaking changes in Node.js core or its dependencies over time.
- gotcha Vorpal v1.x is a CommonJS module. Attempting to use `import` statements directly in an ES Module project without proper CommonJS interoperability configuration will lead to errors.
- gotcha The `action` callback for commands requires `callback()` to be called at the end of the asynchronous operation, otherwise the Vorpal prompt will not return, appearing to hang.
Install
-
npm install vorpal -
yarn add vorpal -
pnpm add vorpal
Imports
- vorpal
import vorpal from 'vorpal'; // OR import { vorpal } from 'vorpal';const vorpal = require('vorpal')(); - Command instance
const command = vorpal.command('foo'); command.action(...);vorpal.command('foo', 'description').action(function(args, callback) { /* ... */ }); - vorpal.show()
vorpal.delimiter('myapp$'); // ... more code ... vorpal.show();vorpal.show();
Quickstart
const vorpal = require('vorpal')();
vorpal
.command('greet <name>', 'Greets the given name.')
.option('-p, --polite', 'Use a polite greeting.')
.action(function(args, callback) {
const greeting = args.options.polite ? 'Hello there, ' : 'Hi, ';
this.log(`${greeting}${args.name}!`)
callback();
});
vorpal
.command('add <numbers...>', 'Adds a list of numbers.')
.action(function(args, callback) {
const sum = args.numbers.reduce((acc, num) => acc + parseFloat(num), 0);
this.log(`The sum is: ${sum}`);
callback();
});
vorpal
.delimiter('mycli$')
.show();