Skip to main content
Early access — new tools and guides added regularly
🔵 Build Real Projects — Guide 6 of 8
View track
>_ claude codeIntermediate35 min

Build and Publish a CLI Tool

Create a command-line tool that others can install with npm. Learn argument parsing, interactive prompts, and npm publishing.

What you will build
A published npm package that anyone can install and use

Designing a useful CLI tool

The best CLI tools solve a specific, recurring problem. Think about what you do repeatedly in your terminal. For this guide, we will build a project scaffolding tool — it creates a new project with your preferred stack, configuration files, and initial structure. But the patterns apply to any CLI. Ask Claude Code: "Create a Node.js CLI tool called 'create-my-stack' that scaffolds a new project. It should interactively ask: project name, framework (Next.js, Express, Astro), include TypeScript (yes/no), include Tailwind (yes/no), and Git init (yes/no). Use Commander for argument parsing and Inquirer for interactive prompts. Create the project with the selected options."

Argument parsing, flags, and interactive prompts

A good CLI tool works both interactively and non-interactively. Ask Claude Code: "Add command-line flags for all the interactive options: --name, --framework, --typescript, --tailwind, --git. If flags are provided, skip the corresponding prompts. If no flags are provided, show all prompts. Add --help with usage examples. Add --version that reads from package.json." Claude Code will make the tool flexible — power users can run it with all flags for instant scaffolding, while newcomers get guided prompts.

Testing, linting, and preparing for npm

Before publishing, the tool needs polish. Ask Claude Code: "Add unit tests for the scaffolding logic (test that each framework option creates the correct files). Add a --dry-run flag that shows what would be created without creating anything. Add a progress spinner for longer operations. Handle errors gracefully — if the directory already exists, warn and exit instead of crashing."

Publishing to npm

Make it available to the world. Ask Claude Code: "Prepare this for npm publishing. Set up the package.json with the correct bin field, files field, keywords, repository URL, and license. Create a README with installation instructions, usage examples, and a feature list. Add a postinstall message welcoming users." Then: npm login, npm publish. Anyone can now run npx create-my-stack to use your tool. You just published open source software.

Related Lesson

Open Source and Distribution

This guide is hands-on and practical. The full curriculum covers the conceptual foundations in depth with structured lessons and quizzes.

Go to lesson