The Wayback Machine - https://web.archive.org/web/20241127233745/https://www.geeksforgeeks.org/simple-task-manager-cli-using-node-js/
Open In App

Simple Task Manager CLI using Node.js

Last Updated : 06 Apr, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.

Simple Task Manager CLI using Node.js

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:

Simple Task Manager CLI using Node.js

 

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.

Simple Task Manager CLI using Node.js

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');
 
// Add a todo item
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;
    }
};
 
// Delete a todo item
const deleteTodo = (title) => {
    let todos = fetchTodos();
    let filteredtodos = todos.filter(
        (todo) => todo.title !== title);
    saveTodos(filteredtodos);
 
    return todos.length !== filteredtodos.length;
};
 
 
// Read a todo item
const readTodo = (title) => {
    let todos = fetchTodos();
    let filteredTodos = todos.filter(
        (todo) => todo.title === title);
    return filteredTodos[0];
};
 
 
// List all todo items
const listTodos = () => {
    return fetchTodos();
};
// Utility functions
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}`);
};
 
// Exporting function
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"  

Simple Task Manager CLI using Node.js

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:

Simple Task Manager CLI using Node.js

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"  

Simple Task Manager CLI using Node.js

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:

Simple Task Manager CLI using Node.js

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"  

Simple Task Manager CLI using Node.js

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  

Simple Task Manager CLI using Node.js



Previous Article
Next Article

Similar Reads

Task Manager App using MERN Stack
Task Manager is very crucial to manage your tasks. In this article, we are going to develop a task manager application using the MERN stack. This application helps users to manage their tasks efficiently, offering essential features like creating new tasks, editing existing ones, and deleting tasks as needed. We'll walk through the step-by-step pro
10 min read
Task Manager App using Express, React and GraphQL.
The Task Manager app tool is designed to simplify task management with CRUD operation: creation, deletion, and modification of tasks. Users can easily generate new tasks, remove completed ones, and update task details. In this step-by-step tutorial, you will learn the process of building a Basic Task Manager App with Express, React and GraphQL. Pre
6 min read
Create a Task Manager App using React-Native
In this article, we'll walk you through the process of building a basic Task Manager app using React Native. The application enables users to effortlessly create, edit, complete/incomplete, and delete­ tasks, providing an uncomplicated yet impactful introduction to Re­act Native's mobile app development capabilities. PrerequisitesIntroduction to Re
7 min read
Create a COVID-19 Tracker CLI using Node.js
In this article, we will see how to create a command-line Corona Virus Tracker using Node.js. We will track total cases, active cases, totally recovered cases, and total deaths in the Indian States. Approach: We use an npm package called 'request' to fetch data from publicly available covid-19 API https://api.covid19india.org/data.json. We will cle
2 min read
How to Create a CLI Based Calculator App using Node.js ?
Creating a Command-Line Interface (CLI) based calculator app using Node.js is a great way to get hands-on experience with Node.js and enhance your understanding of JavaScript in a server-side context. In this article, we will walk through the steps to build a simple yet functional calculator that can perform basic arithmetic operations such as addi
4 min read
Todo List CLI application using Node.js
CLI is a very powerful tool for developers. We will be learning how to create a simple Todo List application for command line. We have seen TodoList as a beginner project in web development and android development but a CLI app is something we don't often hear about. Pre-requisites:A recent version of Node.js downloaded and installed.A text editor,
13 min read
Task Management System using Node and Express.js
Task Management System is one of the most important tools when you want to organize your tasks. NodeJS and ExpressJS are used in this article to create a REST API for performing all CRUD operations on task. It has two models User and Task. ReactJS and Tailwind CSS are used to create a frontend interface part in which we can add, delete, and update
15+ min read
How to build your own CLI (Command Line Interface) with Node.js ?
Introduction: A command-line interface (CLI) is a text-based user interface (UI) for running programs, managing files, and interacting with computers. Building your own CLI is easier than you might think with Node.js. There are a bunch of open-source packages that can handle color, animation, and user input with ease. In this article, we will be bu
2 min read
How to Build a JavaScript Command Line Interface (CLI) with Node.js
In this article, we will see how to create a JavaScript CLI with the help of NodeJs. JavaScript being an interpreter language, it is a very dynamic language, due to which it provides us with many facilities, due to which many tasks become very easy and anyone can understand them very easily, so in today's era JavaScript It is one of the most popula
4 min read
Task Scheduling App with Node and Express.js
Task Scheduling app is an app that can be used to create, update, delete, and view all the tasks created. It is implemented using NodeJS and ExpressJS. The scheduler allows users to add tasks in the cache of the current session, once the app is reloaded the data gets deleted. This can be scaled using databases like MongoDB and authentication and va
4 min read
What are the advantages of using create-react-app CLI ?
Introduction: To get started with any framework you need to understand what all files you need to install. With React you don't need to worry about this. React will install all the files required for you to get started with a single command. It will have all the necessary packages installed and you can start rendering and building your project at o
3 min read
What are some limitations of using react-native-cli for instantiating a project ?
React Native CLI helps you to set up a react native project and provide control over the management of the project locally. Syntax to create a project: npx react-native init ProjectName Though react-native-cli is not the only way to create a react-native project. Expo is another bundle of tools that help you create a react-native project in the fas
3 min read
How do you run Postman collections using Newman CLI?
Postman is an API(application programming interface) development tool that helps to build, test and modify APIs.  In this tutorial, we will see how to run Postman collections using Newman CLI(Command Line Interface) Prerequisites:Node JS installed in your systemKnowledge about how APIs worksStep 1 : Checking of Node version Newman is built on Node.
2 min read
Introduction to JavaScript Course - Learn how to build a task tracker using JavaScript
This is an introductory course about JavaScript that will help you learn about the basics of javascript, to begin with, the dynamic part of web development. You will learn the basics like understanding code structures, loops, objects, etc. What this course is about? In this course we will teach you about the basics of the scripting language i.e) Ja
4 min read
Task Scheduler Using HTML, CSS and JS
In this article, we will create a Task Scheduler web application using HTML, CSS and JavaScript. This is an application which can store tasks provided by user and classified them as low priority, middle priority, and high priority. User also can provide a deadline for the task. User also can mark done by highlighting them. This all-functional logic
3 min read
Task Scheduler using React
Task Schedular is an application that saves tasks submitted by the user and categorizes them among low, middle, or high priority. The user can also provide a deadline for the task. The user can also mark a task as completed by clicking on the button, and it will be added to the completed task area. Preview of Final Output: Let us have a look at how
5 min read
Build a Task Management App using Next JS
A Task management app is a useful web application that assists in efficiently organizing and managing tasks. It provides various functionalities such as creating tasks, assigning prioritie­s and deadlines, marking complete tasks, and enabling task search based on ke­ywords and priorities. Preview of final output: Let us have a look at how the final
5 min read
How to build a simple Discord bot using Node.js ?
Discord is an instant messaging application mostly used by developers and gamer communities. Many discord servers use bots to automate the task. Bots are programs that allow us to automate some tasks like messaging, maintaining our server, etc. Discord provides us with many built-in bots. Discord also allows us to build our own bots. For javascript
3 min read
Creating a simple JSON based API using Node.js
API (Application Programming Interface) results are commonly used by applications, sometimes to withdraw information from their huge databases, check the status of a particular data, or sometimes they depend on third-party APIs to display additional information related to users. Here we will make a simple public JSON based Chemistry API, which can
3 min read
Using Restify to Create a Simple API in Node.js
Restify is an npm package that is used to create efficient and scalable RESTful APIs in Nodejs. The process of creating APIs using Restify is super simple. Building a RESTful API is a common requirement for many web applications. Restify is a popular Node.js framework that makes it easy to create RESTful services. It’s lightweight, fast, and specif
6 min read
Music Playlist Manager with Node.js and Express.js
In this article, we’ll walk through the step-by-step process of creating a Music Playlist Manager with NodeJS and ExpressJS. This application will provide users with the ability to register, log in, create playlists, add tracks to playlists, update playlists, delete playlists, and manage their user profile. We'll also implement authentication using
14 min read
How to generate Components in a specific folder with Angular CLI ?
Angular CLI is a command-line interface tool. Angular applications can be directly created, initialized, developed, and maintained using the command shell provided by CLI. Angular CLI facilitates to generate any number of components in the application, & by default, the Components are generated inside the app folder in the source(src) directory
3 min read
What is npm CLI ?
npm CLI is a combination of words where npm refers to Node Package Manager and CLI refers to the Command Line Interface . Using the command line we are trying to access the packages and dependencies of the NodeJS. npm CLI provides different commands for developers to install, delete, and manage. Table of Content npm CLI commandsFeatures pf npm CLIB
2 min read
CLI Commands in NPM
NPM, short for Node Package Manager, is the default package manager for NodeJS. It is a command-line utility that allows you to install, manage, and share packages or modules of JavaScript code. These packages can range from small utility libraries to large frameworks, and they can be easily integrated into NodeJS projects to extend their functiona
4 min read
Angular CLI - Overview and Reference
Angular is the most popular front-end framework for building dynamic web applications. It uses typescript by default for creating logic and methods for a class. In this article, we'll explore the fundamentals of Angular CLI, its basic workflow, workspace structure, and see into a comprehensive command reference for various tasks. What is Angular CL
3 min read
Bulma with Sass CLI
In this article, we'll see how to use Bulma with Sass CLI. Bulma elements come with pres-styled components, what if you want to modify or customize them according to your requirement? You can do that just by using sass. One way to achieve this is by using SASS-CLI, which is a sass command line from which we can create a CSS file using sass files to
4 min read
Introduction and Installation of Heroku CLI on Windows machine
Heroku: It is a cloud based application deployment and management service. Heroku works on the container based design system and these smart containers are known as dynos . It runs application inside various dynos and each dyno is separated from each other. Installation of Heroku on Windows machine:Step 1: Download Windows installer Download the ap
2 min read
How To Deploy a Django Application to Heroku with Git CLI?
Deploying a Django application to Heroku using the Git command-line interface (CLI) is a simple process. Heroku provides a platform-as-a-service (PaaS) that enables you to deploy, manage, and scale your applications easily. This guide will walk you through the steps to deploy your Django application to Heroku using Git CLI. Deploying Django project
3 min read
Build an App with Angular and Angular CLI
Angular is a powerful front-end framework that allows developers to create robust web applications. Angular is based on the Model-View-Controller (MVC) architecture and allows for the creation of reusable components, efficient state management, and two-way data binding. The Angular CLI (Command Line Interface) simplifies project setup and developme
3 min read
How to Change Port Number in vue-cli Project ?
In this article, we will see the detailed step-by-step process to change the port number in the Vue-Cli project. We will start straight by creating a new Vue project, and move onto the process to change the port number. We will also see the complete verification of changes done in port configuration. ApproachIn this approach, we are going to change
2 min read
three90RightbarBannerImg