Simple Interactive CLI App with Node.js

 by Robin Wieruch
 - Edit this Post

This tutorial is part 2 of 2 in the series.

In this tutorial, you will learn how to build a simple interactive CLI app with Node.js and TypeScript. The app will create a chat interface that reads user input and responds interactively. Let's get started!

// src/index.ts
import readline from "readline";
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const chat = () => {
rl.question('Enter a command (type "exit" to quit): ', (input) => {
if (input.toLowerCase() === "exit") {
console.log("Goodbye!");
rl.close();
} else {
console.log(`You entered: ${input}`);
chat();
}
});
};
console.log("Welcome to the CLI App!");
chat();

To run the app, execute the following command in your terminal:

// Command Line
npm run dev

This will start the CLI app and display the welcome message. You can now enter commands and interact with the app. To exit the app, type "exit" and press Enter. The app will display a goodbye message and close the interface.

That's it! You have successfully built a simple interactive CLI app with Node.js and TypeScript. Feel free to customize the app further and add more interactive features. Happy coding!

Keep reading about 

The article is a short how to add Sass support to your create-react-app application . It shows you how to setup Sass, but also how to use it in your components. You will learn how to style a specific…

Just recently I had to use Docker for my create-react-app web application development. Here I want to give you a brief walkthrough on how to achieve it. First of all, we need a React application…

The Road to React

Learn React by building real world applications. No setup configuration. No tooling. Plain React in 200+ pages of learning material. Learn React like 50.000+ readers.

Get it on Amazon.