Sequence NODE_222
Medium

Build a CLI Tool to Count Words in a File

Node.js
Technical Specification

Create a Node CLI script that takes a file path from command line arguments and prints the word count.

Input/Output Samples
Input:node wc.js notes.txt
Output:Words: 120
Optimal Logic Path
const fs = require("fs");

const file = process.argv[2];
if (!file) {
  console.error("Usage: node wc.js <file>");
  process.exit(1);
}
const text = fs.readFileSync(file, "utf8");
const words = text.trim().split(/s+/).filter(Boolean);
console.log("Words:", words.length);
Architectural Deep-Dive
We read the file synchronously, split by whitespace, filter empty strings, and count the result.