Loading

Quipoin Menu

Learn • Practice • Grow

node-js / CLI Notes App
tutorial

CLI Notes App

Now that you've learned all the core concepts of Node.js, it's time to build something real! In this project, we'll build a command-line notes application. You'll be able to add, list, read, and delete notes – all from your terminal.

Project Overview: CLI Notes App

Our notes app will be a command-line tool that lets users:
  • Add a new note (with a title and body)
  • List all notes (show titles)
  • Read a specific note (show title and body)
  • Remove a note

This project will use the `fs` module to store notes as JSON files, `path` module for file paths, and `process.argv` to handle command-line arguments.

Step 1: Project Setup
mkdir notes-app
cd notes-app
npm init -y

Step 2: Understanding Command-Line Arguments

When you run `node app.js add --title="Shopping" --body="Buy milk"`, the arguments are available in `process.argv`. We'll use a simple approach: the first argument after the script name is the command (add, list, read, remove), and subsequent arguments are parsed as key-value pairs.
<!-- app.js -->
const fs = require('fs');
const path = require('path');

const notesFile = path.join(__dirname, 'notes.json');

<!-- Helper to read notes -->
function loadNotes() {
  try {
    const dataBuffer = fs.readFileSync(notesFile);
    const dataJSON = dataBuffer.toString();
    return JSON.parse(dataJSON);
  } catch (e) {
    return [];
  }
}

<!-- Helper to save notes -->
function saveNotes(notes) {
  const dataJSON = JSON.stringify(notes, null, 2);
  fs.writeFileSync(notesFile, dataJSON);
}

Step 3: Adding a Note
function addNote(title, body) {
  const notes = loadNotes();
  const duplicateNote = notes.find(note => note.title === title);
 
  if (!duplicateNote) {
    notes.push({
      title: title,
      body: body
    });
    saveNotes(notes);
    console.log('New note added!');
  } else {
    console.log('Note title already taken!');
  }
}

Step 4: Listing Notes
function listNotes() {
  const notes = loadNotes();
  console.log('Your notes:');
  notes.forEach(note => {
    console.log(' -', note.title);
  });
}

Step 5: Reading a Note
function readNote(title) {
  const notes = loadNotes();
  const note = notes.find(note => note.title === title);
 
  if (note) {
    console.log(note.title);
    console.log('---');
    console.log(note.body);
  } else {
    console.log('Note not found!');
  }
}

Step 6: Removing a Note
function removeNote(title) {
  const notes = loadNotes();
  const notesToKeep = notes.filter(note => note.title !== title);
 
  if (notes.length > notesToKeep.length) {
    saveNotes(notesToKeep);
    console.log('Note removed!');
  } else {
    console.log('Note not found!');
  }
}

Step 7: Parsing Command-Line Arguments
const command = process.argv[2];

if (command === 'add') {
  const title = process.argv[3];
  const body = process.argv[4];
  addNote(title, body);
} else if (command === 'list') {
  listNotes();
} else if (command === 'read') {
  const title = process.argv[3];
  readNote(title);
} else if (command === 'remove') {
  const title = process.argv[3];
  removeNote(title);
} else {
  console.log('Unknown command. Use: add, list, read, remove');
}

Running the App
node app.js add "Shopping List" "Milk, Eggs, Bread"
node app.js list
node app.js read "Shopping List"
node app.js remove "Shopping List"

Two Minute Drill

  • We built a CLI notes app using Node.js built-in modules.
  • Used `fs` to read/write a JSON file for persistent storage.
  • Used `process.argv` to parse command-line arguments.
  • Implemented add, list, read, and remove functionality.
  • This project demonstrates practical file I/O and CLI argument handling.

Need more clarification?

Drop us an email at career@quipoinfotech.com