Meow CLI Helper
Meow is a minimalist and dependency-free helper library designed for creating command-line interface (CLI) applications in Node.js. It simplifies argument parsing, flag handling, and generation of help and version output. Currently stable at version 14.1.0, Meow maintains an active release cadence, often aligning major versions with Node.js LTS releases to keep up with runtime changes. Its key differentiators include automatic conversion of flags to camelCase, support for `--no-` prefix flag negation, and built-in `--version` and `--help` handling. Since version 12, Meow has been ESM-only, requiring modern Node.js module syntax and an `import.meta` object for package metadata resolution. Recent additions include robust subcommand parsing via the `commands` option and the ability to make input arguments required. It's an opinionated, lightweight choice for CLI parsing.
Common errors
-
ERR_REQUIRE_ESM: require() of ES Module .../node_modules/meow/index.js from ... not supported.
cause Attempting to import `meow` using CommonJS `require()` syntax in an ES Module context, or in a CommonJS project that doesn't correctly handle ESM imports.fixRefactor your application to use ES Modules (`import meow from 'meow';`) and ensure your `package.json` has `"type": "module"`. If using TypeScript, set `"module": "node16"` or `"nodenext"` and `"moduleResolution": "node16"` or `"nodenext"` in `tsconfig.json`. -
Error: The `importMeta` option is required.
cause The `importMeta: import.meta` option was omitted from the `meow` constructor call.fixAdd `{ importMeta: import.meta }` to the `meow` constructor. This property is crucial for `meow` to correctly resolve paths to your `package.json` for displaying help and version information. -
Error [ERR_OS_NOT_SUPPORTED]: Your current Node.js version is X.Y.Z, but meow requires >=20. The latest Node.js LTS version is recommended.
cause The installed Node.js version does not meet the minimum requirement specified by `meow`.fixUpgrade your Node.js environment to version 20 or higher. You can use tools like `nvm install 20` or `volta install node@20`. -
TypeError: 'alias' is not a valid flag option. Did you mean 'shortFlag'?
cause An older `meow` configuration or code attempting to use the `alias` property within flag definitions after `meow` v12, where it was renamed.fixReplace the `alias` property with `shortFlag` in your flag definitions (e.g., `shortFlag: 'r'` instead of `alias: 'r'`). If you need multiple aliases, use the new `aliases` option. -
Error: Input required.
cause The `input.isRequired` option is set to `true` in the `meow` configuration, but no non-flag arguments were provided when running the CLI.fixEnsure that the necessary input arguments are always provided to the CLI. If the input is not strictly mandatory in all scenarios, consider setting `input.isRequired: false` or making it a function that dynamically determines requirement.
Warnings
- breaking Meow v14 and later require Node.js 20 or newer. Earlier versions of Node.js are no longer supported.
- breaking Meow is an ECMAScript Module (ESM) only package since v12. Attempting to use `require()` will result in a runtime error (`ERR_REQUIRE_ESM`).
- breaking The `alias` flag option was renamed to `shortFlag` in v12 to provide more precise terminology. A separate `aliases` option is now available for actual multi-character aliases.
- breaking The `importMeta` option is now a required configuration property for the `meow` constructor (since v12). It is used to correctly locate the `package.json` for version and help information.
- gotcha The `hardRejection` option was removed in v13.1.0 because its functionality became the default behavior for unhandled promise rejections in Node.js 16 and later.
Install
-
npm install meow -
yarn add meow -
pnpm add meow
Imports
- meow
const meow = require('meow');import meow from 'meow';
- AnyFlag
import type { AnyFlag } from 'meow'; - AnyFlags
import type { AnyFlags } from 'meow';
Quickstart
#!/usr/bin/env node
import meow from 'meow';
const cli = meow(`
Usage
$ foo <input>
Options
--rainbow, -r Include a rainbow
Examples
$ foo unicorns --rainbow
🌈 unicorns 🌈
`, {
importMeta: import.meta, // This is required for meow to find package.json
flags: {
rainbow: {
type: 'boolean',
shortFlag: 'r'
}
}
});
// Example usage with parsed input and flags
const inputArgument = cli.input.at(0);
const enableRainbow = cli.flags.rainbow;
if (inputArgument) {
const message = enableRainbow ? `🌈 ${inputArgument} 🌈` : inputArgument;
console.log(message);
} else {
console.log('No input provided. Try: foo unicorns --rainbow');
cli.showHelp();
}