Unlock Your Python Backend Career: Build 30 Projects in 30 Days. Join now for just $54

Building a Node.js Project Starter Template CLI Tool

by Ibukun oluwaseyi

.

Updated Sun Apr 06 2025

.
Building a Node.js Project Starter Template CLI Tool

Starting a new software project can be repetitive and time-consuming.

Developers often need to create a project structure, add configuration files, and set up dependencies.

This is referred to as boilerplate code, which can be reused across projects.

While many frameworks offer CLI commands to simplify project creation, they might not cover every specific use case.

For example, developers may want to reuse configurations for linting, databases, or code snippets across multiple projects.

Repeating these tasks for every new project can be error-prone and tedious. A solution to streamline this process is to create a CLI tool that generates project starters and code snippets based on specific needs.

In this article, you will learn how to build a project starter template CLI using Node.js, readline-sync, colors, and fs-extra. By the end, you'll know how to create your own project template generator CLI tool.

Prerequisites

Before getting started, make sure you are familiar with the following:

  • Node.js

  • JavaScript

  • NPM

  • Basic Linux command line usage

Let's Get Started

We'll walk through the process of building a Node.js project starter template CLI tool. Follow these steps:

1. Create a New Node.js Project

Open a terminal and create a new folder called nodejs-project-cli. Then, navigate to this folder and initialize a Node.js project with the following command:

npm init -y

This command creates a package.json file with default settings.

2. Install Dependencies

To set up the necessary dependencies, run the following command in your terminal:

npm install colors readline-sync fs-extra --save

This installs the plugins needed for our CLI tool.

3. Set Up the Project Folder Structure

Open the project folder in your preferred text editor. In the root directory, create a folder called templates. This folder will contain all the project templates that you want to generate with the CLI tool.

Next, create a folder named simple-express-app inside the templates folder. This will be one of the templates you can generate.

4. Create the CLI Script

Inside the root directory, create a file named index.js and add the following code:

#!/usr/bin/env node
require('colors')
const readLineSync = require("readline-sync")
const path = require('path')
const fse = require('fs-extra')

const NO_CHOICE_MADE = -1
const CURR_DIR = process.cwd();
const templatesDir =  path.join(__dirname,'template')
const templates = fse.readdirSync(templatesDir)

if (!templates.length) {
   console.log('No template to choose from, templates folder is empty');
   process.exit(0)
}  

const index = readLineSync.keyInSelect(templates)

if (index === NO_CHOICE_MADE) {
   process.exit(0);
}

const projectName = readLineSync.question('What is the name of your project? ', {
   limit: input => input.trim().length > 0,
   limitMessage: 'The project has to have a name, try again'
});

const confirmCreateDirectory = readLineSync.keyInYN(`You entered '${projectName}', create directory with this name?`);

if (confirmCreateDirectory) {
   const template = templates[index];
   const source = path.join(templatesDir, template);
   const destination = path.join(CURR_DIR, projectName);
   fse.copy(source, destination)
     .then(() => console.log(`Successfully created ${destination}`.green))
     .catch(err => console.error(err));
 } else {
   console.log('Aborted creating a new template');
 }

This script prompts the user to select a template, enter a project name, and copy the selected template to a new directory.

5. Add Template Files

Now, add the necessary starter files to the simple-express-app folder inside the templates folder. You can include additional folders or files as needed, depending on how many templates you want to include.

6. Make the Script Executable

To run the CLI tool globally, you need to make the script executable. Open the package.json file and add the following snippet:

"bin": {
   "generateproject": "./index.js"
}

This configures the CLI tool to be run as generateproject from the terminal.

7. Install the CLI Tool Globally

In the terminal, run the following command to install the tool globally:

sudo npm link

This makes the generateproject command accessible anywhere on your system.

8. Test the CLI Tool

To test the CLI, open your terminal and run:

generateproject

This will launch the project generator and prompt you to select a template and enter a project name.

9. Add More Templates

You can continue to add more project templates to the templates folder. Each template should be a separate folder with all the necessary files for that project. After adding a new template, you can generate it in any new project.

10. Uninstall the CLI Tool

If you ever need to uninstall the tool, run the following command:

sudo npm unlink

Alternatively, you can uninstall it via:

sudo npm uninstall

Conclusion

Congratulations! You've successfully created a powerful CLI tool that allows you to generate project starters. While there are other tools with more features, having your custom tool ensures it perfectly fits your needs.

Course image
Become a Nodejs Backend Engineeer today

All-in-one Nodejs course for learning backend engineering with Nodejs. This comprehensive course is designed for Nodejs developers seeking proficiency in Nodejs.

Start Learning Now

Whenever you're ready

There are 4 ways we can help you become a great backend engineer:

The MB Platform

Join 1000+ backend engineers learning backend engineering. Build real-world backend projects, learn from expert-vetted courses and roadmaps, track your learnings and set schedules, and solve backend engineering tasks, exercises, and challenges.

The MB Academy

The “MB Academy” is a 6-month intensive Advanced Backend Engineering BootCamp to produce great backend engineers.

Join Backend Weekly

If you like post like this, you will absolutely enjoy our exclusive weekly newsletter, Sharing exclusive backend engineering resources to help you become a great Backend Engineer.

Get Backend Jobs

Find over 2,000+ Tailored International Remote Backend Jobs or Reach 50,000+ backend engineers on the #1 Backend Engineering Job Board

Backend Tips, Every week

Backend Tips, Every week