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.
Step 4: After writing the code in the index.js file run this file using the following command:
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
- 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:
In the above file included in the utility section which handles all the NodeJS file management system like shown below:
- readFileSync: https://www.geeksforgeeks.org/node-js-fs-readfilesync-method/
- writeFileSync: https://www.geeksforgeeks.org/node-js-fs-writefilesync-method/
Step 7: Now update our previous index.js file code with the following complete code:
Step 8: Following are some command 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 create 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 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 completed, then use the following command:
node index.js listTodos