| name | nodejs-cli-best-practices |
| description | Guide and audit Node.js CLI application development against 41 established best practices covering UX, distribution, interoperability, accessibility, testing, error handling, development setup, analytics, versioning, and security. Use this skill when building, extending, reviewing, or scaffolding a Node.js CLI โ including when someone says "audit my CLI", "review my CLI code", "I'm building a CLI tool", or asks about adding argument parsing, help output, shell completion/autocomplete, error handling, color output, STDIN, --json flags, exit codes, --version flags, or npm publishing. Applies even when best practices are not explicitly mentioned. Also trigger for "how should I implement X in my CLI" or "what's the right way to do Y in a Node.js CLI". Do NOT use for Node.js backend or API development with no CLI entry point. |
| metadata | {"version":"1.0.0","category":"nodejs","tags":["nodejs","cli","best-practices","audit","command-line"]} |
This skill operates in two modes โ audit for reviewing existing CLI code and development guide for building new features or tools.
Critical
Always read references/best-practices.md before producing any output. It contains the complete reference for all 41 practices with code examples and recommended packages. It is the source of truth for both modes โ do not rely on general knowledge alone, as several practices (specific package recommendations, ยงX.X numbering, configuration precedence order) are specific to this guide.
Determining the mode
| Trigger | Mode | Action |
|---|
| User provides existing CLI code or files to review | Audit | Produce a structured audit report |
| User asks how to build or implement something | Development guide | Surface relevant practices with examples |
| User says "review and help me improve" | Both | Audit first, then provide concrete guidance for each failing practice |
Audit mode
Systematically compare the provided code against the practices in references/best-practices.md. Focus on what is verifiable from the code. Use judgment to mark practices as not applicable (โ) when there's genuinely no way to assess them from static analysis (e.g., Docker support when the project shows no distribution intent).
Prioritize by user impact โ a missing exit code (ยง6.4) or absent --version flag (ยง9.1) affects every user and every CI pipeline; missing Docker image (ยง4.1) is low priority for most projects.
Audit report format
## Node.js CLI Best Practices Audit
### Summary
โ
N practices followed โ ๏ธ N need attention โ N not implemented โ N not applicable
---
### 1. Command Line Experience
| # | Practice | Status | Finding |
|---|----------|--------|---------|
| 1.1 | Respect POSIX args | โ
| Uses yargs with proper short/long aliases |
| 1.2 | Build empathic CLIs | โ | No interactive fallback when required args absent |
...
[Repeat for each section with applicable practices]
---
### Priority recommendations
**High priority** (user-facing or CI-breaking):
- **ยง6.4 Exit codes** โ `process.exit()` called without a code
```js
// Fix
process.exit(1); // on error
process.exit(0); // on success
Medium priority:
Low priority / nice to have:
Keep findings concise and tied to specific code patterns. Always include a concrete fix with code for each โ.
---
## Development guide mode
When building a new CLI feature or tool, don't wait to be asked โ surface relevant best practices immediately based on what the user is building.
**For a "building a CLI from scratch" request**, organize guidance by development phase:
1. **Project setup** (ยง7.1, ยง7.3, ยง4.4, ยง2.2): bin object, shebang, files field, shrinkwrap
2. **Argument design** (ยง1.1, ยง1.2, ยง1.7, ยง1.9, ยง3.4): POSIX compliance, empathic fallbacks, zero-config, help output, config precedence
3. **I/O and interoperability** (ยง3.1, ยง3.2, ยง3.5, ยง3.6, ยง3.7, ยง4.2): STDIN, structured output, gated interactivity, STDOUT/STDERR separation, shell completion, graceful degradation
4. **Error handling** (ยง6.1โยง6.5): trackable codes, actionable messages, debug mode, exit codes
5. **UX polish** (ยง1.4, ยง1.5, ยง1.6): colors, rich interactions, hyperlinks
6. **Versioning** (ยง9.1โยง9.7): `--version` flag, semver, changelog
7. **Security** (ยง10.1): argument injection
**For a targeted feature request** (e.g., "add error handling", "add a --json flag"), surface only the directly relevant practices.
### Development guidance format
Node.js CLI best practices for [feature/topic]
Checklist
Implementation
[Concrete, copy-pasteable code]
Recommended packages
package-name โ when and why to use it
npm install package-name
Common mistakes
- What not to do โ why it breaks โ what to do instead
---
## Quick reference: which section applies
| Topic | Sections |
|-------|----------|
| Argument parsing / flags | ยง1.1, ยง1.7, ยง1.9, ยง3.4 |
| Help / usage output | ยง1.9 |
| Shell completion / autocomplete | ยง3.7 |
| Prompts / interactivity | ยง1.2, ยง1.5, ยง3.5 |
| Colors / styling | ยง1.4, ยง4.2 |
| STDIN / piping | ยง3.1, ยง3.5, ยง3.6 |
| STDOUT / STDERR | ยง3.6 |
| JSON / structured output | ยง3.2, ยง3.6, ยง4.2 |
| Cross-platform issues | ยง3.3, ยง7.2 |
| Error messages | ยง3.6, ยง6.1, ยง6.2 |
| Exit codes | ยง6.4 |
| Debug / verbose mode | ยง3.6, ยง6.3 |
| `--version` flag | ยง9.1, ยง9.3 |
| package.json setup | ยง7.1, ยง7.3, ยง9.3 |
| npm publishing / distribution | ยง2.1, ยง2.2, ยง9.6 |
| Security / user input | ยง10.1 |
| Configuration persistence | ยง1.3, ยง2.3 |
| Node.js version targeting | ยง4.3 |
| Analytics / telemetry | ยง8.1 |
---
## Examples
### Example 1: Audit mode
**User says:** "Audit my Node.js CLI against best practices. Here's my entry file and package.json."
**Actions:**
1. Read `references/best-practices.md`
2. Examine the provided files against each applicable practice
3. Produce the structured audit report with per-section table and priority recommendations
---
### Example 2: Development guide mode (new CLI from scratch)
**User says:** "I'm building a Node.js CLI that parses log files and outputs a summary table. It needs to work cross-platform and I want to publish it on npm."
**Actions:**
1. Read `references/best-practices.md`
2. Identify all practices relevant to this CLI type (cross-platform, npm-published, table output)
3. Organize guidance by development phase (project setup โ argument design โ I/O โ errors โ UX โ versioning โ security)
---
### Example 3: Development guide mode (targeted feature)
**User says:** "My CLI is crashing and just dumping stack traces. How do I add proper error handling?"
**Actions:**
1. Read `references/best-practices.md` sections ยง6.1โยง6.5
2. Surface only the directly relevant practices
3. Provide a concrete, copy-pasteable error handling wrapper
---
## Common mistakes to avoid
| Mistake | Why it matters | Correct approach |
|---------|---------------|------------------|
| Marking ยง4.1 (Docker) as โ for a small personal CLI | Docker is rarely needed; not applicable is honest | Use โ when the practice genuinely doesn't fit the project's scope |
| Only auditing the entry file, ignoring package.json | Several high-impact violations (ยง7.1, ยง7.3, ยง2.1, ยง4.3) live in package.json | Always audit both the entry file and package.json when both are provided |
| Recommending `package-lock.json` for ยง2.2 | The practice specifically calls for `npm-shrinkwrap.json` for published CLIs | Reference `references/best-practices.md` for the exact requirement |
| Skipping the priority grouping in audit reports | Users need to know what to fix first, not just what's wrong | Always include High / Medium / Low priority sections with concrete fixes |
| Providing a development guide without code examples | Prose guidance alone is not actionable | Every recommended practice should include at least one copy-pasteable code block |