Loading

Quipoin Menu

Learn • Practice • Grow

node-js / CLI Notes App
interview

Q1. How would you structure a CLI notes app in Node.js?
Create a command-line tool that accepts commands like add, list, remove, read. Use process.argv to parse arguments. Store notes in a JSON file using fs module. Each note has id, title, body, timestamp. Use chalk for colorful output, yargs for command parsing. Structure: commands in separate files, a notes manager module, and an index.js entry point.

Q2. How do you handle user input in a CLI app?
Use process.argv to get command-line arguments. For more complex parsing, use yargs or commander packages. Example with yargs:
yargs.command('add <body>', 'add a note', (yargs) => {...}, (argv) => { addNote(argv.title, argv.body) }).argv;</div></code></pre>. For interactive prompts, use readline or inquirer.</font></div></div><font size="4"><br></font> <div class="interview-card"><font size="4"><b>Q3.</b> How do you store notes persistently?<br></font><div class="interview-answer"><font size="4">Use the fs module to read/write a JSON file. Example: <pre data-language="javascript"><code class="language-javascript" contenteditable="true"><div style="color: rgb(204, 204, 204); background-color: rgb(31, 31, 31); font-family: Consolas, "Courier New", monospace; line-height: 19px;">const notes = loadNotes(); function loadNotes() { try { return JSON.parse(fs.readFileSync('notes.json')); } catch { return []; } } function saveNotes(notes) { fs.writeFileSync('notes.json', JSON.stringify(notes, null, 2)); }</div></code></pre>. Each note object includes id (using uuid or timestamp), title, body, and timestamps.</font></div></div><font size="4"><br></font> <div class="interview-card"><font size="4"><b>Q4.</b> How do you implement add, remove, list, and read commands?<br></font><div class="interview-answer"><font size="4">add: check for duplicate title, create note object with unique id, push to array, save. remove: filter out note by id or title, save. list: iterate and print titles. read: find note by id/title and print body with formatting. Handle errors like note not found. Use chalk for colored output.</font></div></div><font size="4"><br></font> <div class="interview-card"><font size="4"><b>Q5.</b> How do you make your CLI app globally executable?<br></font><div class="interview-answer"><font size="4">Add a shebang line at the top of your main file: #!/usr/bin/env node. In package.json, add a bin field: <pre data-language="json"><code class="language-json" contenteditable="true"><div style="color: rgb(204, 204, 204); background-color: rgb(31, 31, 31); font-family: Consolas, "Courier New", monospace; line-height: 19px;">"bin": { "notes": "./index.js" }</div></code></pre>. Then run npm link to create a global symlink, or npm publish to distribute. Users can then run notes add "title" "body" from anywhere.</font></div></div>