Simple Task Manager CLI Using NodeJS
A Task Manager is a very useful tool to keep track of your tasks, whether it's for personal use or a work-related project. In this article, we will learn how to build a Simple Task Manager CLI (Command Line Interface) application using Node.js.
What We Are Going to Create?
We will build a CLI task manager that allows users to manage tasks through the terminal. The task manager will have the following features:
- Add a Task: Users can add a task by providing a description.
- List Tasks: View a list of all tasks with their status (completed or not).
- Mark a Task as Completed: Mark a specific task as completed.
- Remove a Task: Remove a task from the task list.
- Persistent Data: Save tasks to a file, so they persist even after closing the application.
Approach
- We will initialize a Node.js project using npm init.
- We will create a model to store tasks with properties like description, completed, and id.
- We will use the built-in fs (File System) module in Node.js to save and load tasks from a file.
- We will use process.argv to capture user input from the command line.
- We will implement various commands (add, list, complete, remove) to manage tasks.
Setting Up Your Node.js Task Manager CLI
Step 1: Create Project Directory
Open your terminal and create a new directory for the task manager project:
mkdir task-manager-cli
cd task-manager-cli
Step 2: Initialize a Node.js Project
Run the following command to initialize the Node.js project:
npm init -y
Step 3: Install Required Modules
For this simple CLI application, we will use the built-in fs (file system) module, so there is no need to install any external dependencies. However, you may install a CLI argument parser (like yargs or commander) if you'd like to improve the usability of the CLI.
To install yargs for better command-line argument parsing, you can run:
npm install yargs
For now, we will proceed without any external libraries.
Step 4: Implementing the Task Manager
We will create the following features in the index.js file:
const fs = require('fs');
const yargs = require('yargs');
const tasksFile = 'tasks.json';
// Helper function to read tasks from the file
const readTasks = () => {
try {
const dataBuffer = fs.readFileSync(tasksFile);
const dataJSON = dataBuffer.toString();
return JSON.parse(dataJSON);
} catch (error) {
return [];
}
};
// Helper function to save tasks to the file
const saveTasks = (tasks) => {
const dataJSON = JSON.stringify(tasks, null, 2);
fs.writeFileSync(tasksFile, dataJSON);
};
// Command to add a task
yargs.command({
command: 'add',
describe: 'Add a new task',
builder: {
description: {
describe: 'Task description',
demandOption: true,
type: 'string'
}
},
handler(argv) {
const tasks = readTasks();
const newTask = {
id: tasks.length + 1,
description: argv.description,
completed: false
};
tasks.push(newTask);
saveTasks(tasks);
console.log(`Task "${argv.description}" added successfully!`);
}
});
// Command to list all tasks
yargs.command({
command: 'list',
describe: 'List all tasks',
handler() {
const tasks = readTasks();
if (tasks.length === 0) {
console.log('No tasks available.');
return;
}
console.log('Task List:');
tasks.forEach((task) => {
console.log(`${task.id}. ${task.description} - ${task.completed ? 'Completed' : 'Not Completed'}`);
});
}
});
// Command to mark a task as completed
yargs.command({
command: 'complete',
describe: 'Mark a task as completed',
builder: {
id: {
describe: 'Task ID to mark as completed',
demandOption: true,
type: 'number'
}
},
handler(argv) {
const tasks = readTasks();
const task = tasks.find((task) => task.id === argv.id);
if (!task) {
console.log('Task not found!');
return;
}
task.completed = true;
saveTasks(tasks);
console.log(`Task ${argv.id} marked as completed!`);
}
});
// Command to remove a task
yargs.command({
command: 'remove',
describe: 'Remove a task',
builder: {
id: {
describe: 'Task ID to remove',
demandOption: true,
type: 'number'
}
},
handler(argv) {
const tasks = readTasks();
const updatedTasks = tasks.filter((task) => task.id !== argv.id);
if (updatedTasks.length === tasks.length) {
console.log('Task not found!');
return;
}
saveTasks(updatedTasks);
console.log(`Task ${argv.id} removed successfully!`);
}
});
// Parse command-line arguments
yargs.parse();
Running the Application
To run the application, use the following commands in your terminal:
Add a Task:
node index.js add --description "Buy groceries"
List All Tasks:
node index.js list
Mark a Task as Completed:
node index.js complete --id 1
Remove a Task:
node index.js remove --id 1

In this example
- Task File Operations: Uses fs to read and write tasks to a tasks.json file, allowing persistence of task data.
- Add Task: The add command lets users add a new task with a description, which is saved in the tasks.json file.
- List Tasks: The list command displays all tasks, showing their description and completion status.
- Complete Task: The complete command marks a specific task as completed by its ID and updates the tasks.json file.
- Remove Task: The remove command deletes a task by its ID from the task list and updates the file accordingly.
Conclusion
In this article, we successfully built a Simple Task Manager CLI using Node.js. By utilizing the built-in fs (File System) module, we created a task manager that allows users to add, list, complete, and remove tasks directly from the terminal. We also implemented persistent storage using a JSON file, ensuring that tasks remain saved even after the application is closed.