First CLI project
Local practice: Node.js 22.16.0 on Linux x64; proposed teaching baseline 26.13.1 remains untested. Copy only the listed files into a fresh disposable directory. No dependency installation or external services required. These are static Node.js examples, not verified ByteMind runner tasks.
Complete CLI: filter synthetic inventory
import { readFile } from 'node:fs/promises';import { report } from './report.mjs';try { const [minimum, ...extra] = process.argv.slice(2); if (!/^(0|[1-9]\d{0,3})$/.test(minimum ?? '') || Number(minimum) > 1000 || extra.length) throw new Error('Usage: node main.mjs MINIMUM (0-1000)'); const text = await readFile(new URL('./stock.json', import.meta.url), 'utf8'); console.log(report(JSON.parse(text), Number(minimum)));} catch (err) { console.error(err.code ? 'Inventory read failed' : err instanceof SyntaxError ? 'Invalid inventory JSON' : err.message); process.exitCode = 1;}export function report(items, minimum) { if (!Array.isArray(items) || items.length > 100 || !Number.isInteger(minimum) || minimum < 0 || minimum > 1000) throw new Error('Invalid inventory or minimum'); const names = new Set(); for (const item of items) { if (!item || typeof item.name !== 'string' || !/^[a-z]{1,30}$/.test(item.name) || names.has(item.name) || !Number.isInteger(item.count) || item.count < 0 || item.count > 1000) throw new Error('Invalid item'); names.add(item.name); } return items.filter(item => item.count >= minimum).map(item => `${item.name}: ${item.count}`).join('\n') || 'No matching items';}[ { "name": "books", "count": 4 }, { "name": "pens", "count": 1 }, { "name": "maps", "count": 3 }]Run:
node main.mjs 3Expected stdout:
books: 4maps: 3Exit status: 0. Expected stderr: empty.
How it works
Separate pure report logic from file reading and argument handling so tests can call the logic without a terminal. The fixed fixture limits records to 100 and counts to 0–1000; names are short lowercase ASCII identifiers and duplicates are rejected. The input file is trusted, small and local, not a general untrusted upload parser. process.argv.slice(2) skips the executable and script. The CLI writes results to stdout, errors to stderr, sets a nonzero exit code for failure, and resolves its data file relative to the module. For an interactive extension, node:readline/promises can ask a question on stdin/stdout; close its interface in finally. Use process.stdin.isTTY to decide whether interactive prompts are suitable; piped input may not be a terminal. Keep plain text available for redirected output.
Reject an invalid command argument
import { readFile } from 'node:fs/promises';import { report } from './report.mjs';try { const [minimum, ...extra] = process.argv.slice(2); if (!/^(0|[1-9]\d{0,3})$/.test(minimum ?? '') || Number(minimum) > 1000 || extra.length) throw new Error('Usage: node main.mjs MINIMUM (0-1000)'); const text = await readFile(new URL('./stock.json', import.meta.url), 'utf8'); console.log(report(JSON.parse(text), Number(minimum)));} catch (err) { console.error(err.code ? 'Inventory read failed' : err instanceof SyntaxError ? 'Invalid inventory JSON' : err.message); process.exitCode = 1;}export function report(items, minimum) { if (!Array.isArray(items) || items.length > 100 || !Number.isInteger(minimum) || minimum < 0 || minimum > 1000) throw new Error('Invalid inventory or minimum'); const names = new Set(); for (const item of items) { if (!item || typeof item.name !== 'string' || !/^[a-z]{1,30}$/.test(item.name) || names.has(item.name) || !Number.isInteger(item.count) || item.count < 0 || item.count > 1000) throw new Error('Invalid item'); names.add(item.name); } return items.filter(item => item.count >= minimum).map(item => `${item.name}: ${item.count}`).join('\n') || 'No matching items';}[ { "name": "books", "count": 4 }, { "name": "pens", "count": 1 }, { "name": "maps", "count": 3 }]Run:
node main.mjs 3abcExpected stdout:
(empty)Exit status: 1. Expected stderr contains: Usage: node main.mjs MINIMUM (0-1000).
Common mistake
Do not parse numbers before validating the entire argument; parseInt can accept a prefix such as 3abc. Do not mix diagnostics with machine-readable stdout. The lab accepts one argument and does not accept a path supplied by the user.
Practice
Run with 0, 3, 5 and an invalid value; expect three rows, two rows, no matching items and an error respectively. Add tests for empty input, duplicates and invalid quantities before adding an interactive menu.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
First CLI project — knowledge check
Apply the concepts and predict the behavior taught in this lesson.
Which module should own filtering and formatting?
Where should usage errors be written?
Why validate the whole numeric argument first?