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;</code></pre><br>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.<br>Example: <pre style="background-color:#2d2d2d; padding:10px; border-radius:4px; overflow-x:auto;"><code style="color:#f8f8f2; font-family: monospace; font-size: 1.2em;">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)); }</code></pre><br>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.<br>remove: filter out note by id or title, save.<br>list: iterate and print titles.<br>read: find note by id/title and print body with formatting.<br>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.<br>In package.json, add a bin field: <pre style="background-color:#2d2d2d; padding:10px; border-radius:4px; overflow-x:auto;"><code style="color:#f8f8f2; font-family: monospace; font-size: 1.2em;">"bin": { "notes": "./index.js" }</code></pre><br>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>