Simple Task Manager CLI using Node.js
Last Updated :
06 Apr, 2023
Before creating a simple Task Manager CLI using NodeJS, let’s run our first basic Node.js application with the following simple steps:
Note: You can download and install the Node.js application from href=”https://nodejs.org/en/”
Step 1: Create a Directory for our Task Manager CLI named TODO-CLI, you can give the name of your choice.

write : mkdir <filename>
Step 2: In that directory make a JavaScript file named index.js as shown below:

Make an index.js file
Step 3: This file contains some JavaScript code to run a program, Now we can write some sample code as shown below:

Step 4: After writing the code in the index.js file run this file using the following command:
node index.js
After running the command you will the following output So, now we confirm that our index.js file is running without any problem.

Step 5: Install the following modules which are used for this application using the following command:
npm install yargs
npm install lodash
- loadsh: A modern JavaScript utility library that helps to work with Array, Numbers, String, Objects, and Strings.
- yargs: It is an interactive command-line module that helps to read the user input.
Step 6: Create a new file called todo.js and write down the following code in it:
Filename: todo.js
Javascript
console.log("todo.js is running !!");
const fs = require('fs');
const addTodo = (title) => {
const todos = fetchTodos();
const todo = {
title
};
const duplicatetodos = todos.filter(
(todo) => todo.title === title);
if (duplicatetodos.length === 0) {
todos.push(todo);
saveTodos(todos);
return todo;
}
};
const deleteTodo = (title) => {
let todos = fetchTodos();
let filteredtodos = todos.filter(
(todo) => todo.title !== title);
saveTodos(filteredtodos);
return todos.length !== filteredtodos.length;
};
const readTodo = (title) => {
let todos = fetchTodos();
let filteredTodos = todos.filter(
(todo) => todo.title === title);
return filteredTodos[0];
};
const listTodos = () => {
return fetchTodos();
};
const fetchTodos = () => {
try {
let todosString =
fs.readFileSync('tasks-data.json');
return JSON.parse(todosString);
} catch (e) {
return [];
}
};
const saveTodos = (todos) => {
fs.writeFileSync('tasks-data.json',
JSON.stringify(todos));
};
const logTodo = (todo) => {
console.log('## ---## --- ##');
console.log(`It's title is: ${todo.title}`);
};
module.exports = {
addTodo,
deleteTodo,
readTodo,
listTodos,
logTodo
};
|
In the above file included in the utility section which handles all the NodeJS file management systems shown below:
Step 7: Now update our previous index.js file code with the following complete code:
Filename: index.js
Javascript
console.log("index.js is running");
const fs = require('fs');
const _ = require('lodash');
const yargs = require('yargs');
const tasks = require('./todo');
const argv = yargs.argv;
const command = argv._[0];
console.log('Running Command is :', command);
if (command === 'addTodo') {
tasks.addTodo(argv.title);
} else if (command === 'deleteTodo') {
let todoDeleted = tasks.deleteTodo(argv.title);
let message = todoDeleted ?
'Todo was deleted' : 'Todo can not found';
console.log(message);
} else if (command === 'readTodo') {
let todo = tasks.readTodo(argv.title);
if (todo) {
console.log('yahoo! The todo was found.');
tasks.logTodo(todo);
} else {
console.log('Oops! The todo was not found.');
}
} else if (command === 'listTodos') {
let allTodos = tasks.listTodos();
console.log(`${allTodos.length} tasks available`);
allTodos.forEach((todo) => tasks.logTodo(todo));
} else {
console.log('It is a Invalid command.');
}
|
Step 8: The following are some commands for our Task Application:
1. Add Tasks: Use the following command to add tasks for our application.
node index.js addTodo --title="your tasks"

When you add all the tasks by the use of the command line, if you look at the project folder you can see a task-data.json file is created and all the tasks are listed as JSON structure in that file as shown below:

2. Delete Tasks: Delete task is necessary when completed the tasks and you wanted to delete from your tasks list, you can delete task using the following command:
node index.js deleteTodo --title="your task"

When you run this command, if you point out in the task-data.json you see which data you typed to delete it remove from the tasks list as shown below:

3. Read Tasks: If you wanted to check whether your all tasks are present or not, use the following command:
node index.js readTodo --title="your task"

4. Display List of Tasks: If you want to see all tasks which not yet been completed, then use the following command:
node index.js listTodos
