DEV Community

Cover image for Building My First npm Package: A CLI for Scaffolding Backend Servers
Terry W
Terry W

Posted on • Edited on

Building My First npm Package: A CLI for Scaffolding Backend Servers

Hello DEV Community! This is both my first npm package and my first DEV article.

After transitioning into full-stack development, I found myself repeating the same setup process whenever I started a new backend project: creating folders, configuring a database connection, adding environment variables, and setting up routes, controllers, and models.

I wanted to automate some of that repetitive work, so I created a CLI tool that scaffolds a backend application with a structured starting point and useful boilerplate.

In this post, I’ll walk through how I built my first npm package and how it can help reduce the time spent setting up a new project.

The Challenge

Many backend projects involve similar initial setup tasks, including creating routes, controllers, data models, database configuration, environment files, and error-handling utilities.

Without automation, developers can end up repeatedly copying boilerplate code or working through the same setup checklist for each project.

My CLI tool aims to reduce that repetition by generating a customisable project structure based on the framework, database, and language selected by the user. Developers still need to provision their database and provide their own credentials, but the initial application structure and connection code are generated for them.

Building the Package: Key Features

Here are some of the main features included in the tool:

  • Multiple database options: Scaffold a project using PostgreSQL, MySQL, SQLite, or MongoDB. Each template includes connection code and configuration suited to the selected database.
  • Generated configuration: The tool creates the project folders, database configuration, example environment variables, and starter application files. You still need to create or provision your database and provide the required credentials.
  • Layered project structure: Routes, controllers, models, database utilities, and error-handling logic are separated into clear folders, creating an MVC-inspired structure that is easier to navigate and maintain.
  • JavaScript and TypeScript support: Choose between JavaScript and TypeScript when generating your project.
  • Express and Hono support: Express templates are available across the supported databases, with Hono templates available for PostgreSQL projects.

Choosing a database and language

How I Built It

I built the package using Node.js and used the prompts library to create an interactive command-line experience. Users can select their framework, database, language, and project configuration through a series of prompts.

The scaffolding approach was inspired by tools such as Vite, which generate projects from predefined templates. I followed a similar pattern by organising separate template folders for different framework, database, and language combinations.

The CLI uses minimist to parse command-line arguments. File operations, including copying templates and creating the generated project, are handled using Node.js file-system utilities and custom helper functions.

One of the main challenges was understanding how to reliably copy and transform the template files into the final generated project structure. I worked through this through experimentation, debugging, and repeated testing of the generated output.

Usage diagram

Usage

To scaffold a new project, run:

npx create-mvc-server@latest
Enter fullscreen mode Exit fullscreen mode

You can also provide the project name directly:

npx create-mvc-server@latest my-project
Enter fullscreen mode Exit fullscreen mode

After running the command, you’ll be prompted to:

  1. Enter a project name.
  2. Choose a server framework.
  3. Select a database:
  • PostgreSQL
  • MySQL
  • SQLite
  • MongoDB
    1. Choose between JavaScript and TypeScript.
    2. Confirm the selected configuration.

Database options

Language options

A generated project may include a structure similar to:

my-project/
├── __tests__/
├── controllers/
├── db/
├── errors/
├── models/
├── routes/
├── utils/
├── app.js
├── listener.js
├── endpoints.json
├── .env-example
└── package.json
Enter fullscreen mode Exit fullscreen mode

The exact files and folders depend on the framework, database, and language selected.

After generation, you will still need to install or review the dependencies, provision your database where required, and add your own environment values.

Next Steps

The package is still evolving, and I plan to continue improving the available templates and generated boilerplate.

Possible future improvements include authentication templates, additional database configurations, more framework options, and stronger testing around the generated output.

I am also interested in exploring templates for other backend ecosystems, including Go-based HTTP servers, rather than limiting the project entirely to Node.js frameworks.

Conclusion

Building this package has been an educational introduction to creating CLI tools, managing project templates, and publishing packages to npm.

It has also helped me better understand command-line argument parsing, file-system operations, package configuration, and the challenges involved in generating reliable project structures.

I plan to continue improving the package and making it a more useful starting point for developers who want to reduce repetitive backend setup.

To try the package or contribute, you can visit the GitHub repository and share your feedback.

Your Feedback Matters

Do you have suggestions for improving the generated templates or ideas for additional features?

Contributions and feedback are welcome. You can find the source code on GitHub.

Top comments (0)