In the world of Node.js development, Command-Line Interface (CLI) tools are powerful utilities that help streamline workflows and automate repetitive tasks. In this blog, weβll walk through the process of creating, testing, and publishing a CLI tool that fetches π statistics about an NPM package.
π§ What Weβre Building
Weβll build a CLI tool called npm-stats-cli
that allows users to retrieve details about any NPM package, such as its name, latest version, description, and homepage.
1οΈβ£ Step 1: Setting Up the Project
First, initialize a new Node.js project:
mkdir npm-stats-cli cd npm-stats-cli npm init -y
Install the dependencies weβll use:
npm install axios yargs
-
axios
: π For making HTTP requests to the NPM registry. -
yargs
: π οΈ For parsing command-line arguments.
2οΈβ£ Step 2: Writing the CLI Tool
Create an index.js
file for the CLI logic. Add the following code:
#!/usr/bin/env node const axios = require('axios'); const yargs = require('yargs'); const getPackageStats = async (pkgName) => { try { const response = await axios.get(`https://registry.npmjs.org/${pkgName}`); const packageData = response.data; console.log(`\nπ¦ Package Stats:`); console.log(`π€ Name: ${packageData.name}`); console.log(`π Version: ${packageData['dist-tags'].latest}`); console.log(`π Description: ${packageData.description}`); console.log(`π Last modified: ${packageData.time.modified}`); console.log(`π Homepage: ${packageData.homepage || 'No homepage available'}\n`); } catch (error) { console.error('β Error fetching package data:', error.message); } }; yargs.command({ command: 'stats', describe: 'Get stats for an NPM package π', builder: { package: { describe: 'Package name', demandOption: true, type: 'string', }, }, handler: (argv) => { getPackageStats(argv.package); }, }).argv;
3οΈβ£ Step 3: Configuring for Global Use
Update your package.json
to include a bin
field:
"bin": { "npm-stats": "./index.js" }
This ensures that the npm-stats
command is available globally when installed.
Link the tool locally for testing:
npm link
Test the command:
npm-stats stats --package axios
4οΈβ£ Step 4: Publishing to NPM
To make your tool available globally, publish it to the NPM registry.
- Log in to your NPM account:
npm login
- Ensure the package name is unique. If itβs taken, choose a different name or use a scoped name:
"name": "@yourusername/npm-stats-cli"
- Publish the package:
npm publish
π Your tool is now available to the world!
π§ Common Issues and Fixes
β Error: E403 Forbidden
This error occurs if the package name is already taken. Rename the package in package.json
and try again.
β Error: EEXIST: file already exists
This happens if an existing file or symlink conflicts with your CLI command. Remove the conflicting file:
rm -f /path/to/conflicting/file
Then reinstall your tool globally.
5οΈβ£ Step 5: Enhancements
Here are a few ways to make your tool even better:
- π Add Error Handling: Improve error messages for better user feedback.
- π Include More Stats: Extend the tool to fetch download statistics or dependency lists.
- π Improve Documentation: Add a detailed
README.md
for users.
π Conclusion
Building a CLI tool like npm-stats-cli
not only improves your understanding of Node.js but also gives you an opportunity to contribute to the developer community. The process of testing, debugging, and publishing a global tool teaches valuable lessons about package management, versioning, and user experience.
π¬ Have you built your own CLI tool? Share your experience in the comments below!
Top comments (0)