Building a Command-Line To-Do List Manager with Node.js

I'm a Student, Developer, Writer and a Tech Geek
Introduction
In this article, we'll explore how to build a simple yet powerful command-line to-do list manager using Node.js. By the end of this tutorial, you'll have a handy tool to manage your tasks efficiently, right from your terminal.
Prerequisites
Before we dive into building our to-do list manager, make sure you have Node.js installed on your system. You can download and install Node.js from the official website: Node.js Official Website
Step 1: Setting Up the Project
First, let's initialize a new Node.js project. Open your terminal and run the following commands:
mkdir todo-list-cli
cd todo-list-cli
npm init -y
Next, install the inquirer package, which we'll use to prompt users for input:
npm install --save inquirer@^8.0.0
Step 2: Writing the Code
Now, let's write the code for our to-do list manager. Create a new JavaScript file named app.js in your project directory, and paste the following code:
const fs = require('fs');
const inquirer = require('inquirer');
const todoFile = 'todo.json';
function loadTasks() {
try {
const data = fs.readFileSync(todoFile, 'utf8');
return JSON.parse(data);
} catch (err) {
return [];
}
}
function saveTasks(tasks) {
fs.writeFileSync(todoFile, JSON.stringify(tasks, null, 4));
}
function addTask() {
inquirer.prompt([
{
type: 'input',
name: 'task',
message: 'Enter task:'
}
]).then(answers => {
const tasks = loadTasks();
tasks.push({ task: answers.task, done: false });
saveTasks(tasks);
console.log('Task added successfully!');
displayTasks();
});
}
function displayTasks() {
const tasks = loadTasks();
console.log('Tasks:');
tasks.forEach((task, index) => {
console.log(`${index + 1}. [${task.done ? 'x' : ' '}] ${task.task}`);
});
}
/* Other functions like markAsDone(), deleteTask(),
and mainMenu() are omitted for brevity.*/
function init() {
mainMenu();
}
init();
Step 3: Running the Application
Save the changes to app.js and run the application by executing the following command in your terminal:
node app.js
You'll be presented with a menu of options to add tasks, list tasks, mark tasks as done, delete tasks, or exit the application.
Conclusion
In this article, we learned how to build a command-line to-do list CLI using Node.js. We covered essential concepts such as file system operations, user input handling, and menu navigation. You can further enhance this project by adding features like task prioritization, due dates, or even syncing tasks with a cloud service. Feel free to customize and extend the application to fit your specific needs. Happy task managing!


