Logo

dev-resources.site

for different kinds of informations.

Building My First NPM Package: A CLI for Scaffolding Express Servers

Published at
1/12/2025
Categories
npm
opensource
webdev
cli
Author
ttibbs
Categories
4 categories in total
npm
open
opensource
open
webdev
open
cli
open
Author
6 person written this
ttibbs
open
Building My First NPM Package: A CLI for Scaffolding Express Servers

Introduction

Hello Dev.to community! Not only is this my first npm package but it is also my first dev.to article. As a developer who recently transitioned into full-stack development, I found the process of setting up Express.js projects slightly tedious and time-consuming. I wanted to automate this repetitive process somewhat, so I created a CLI tool that scaffolds an entire backend application with boilerplate code to help get you started quickly. This post will walk you through my journey of building my first npm package and show how it can save you time on your next project.

The Challenge

Every backend project requires a complex setup process: creating routes, data models, controllers, configuring database connections, and managing environment variables. Without automation, developers can spend valuable time copy-pasting boilerplate code and following the same setup checklist repeatedly. My CLI tool helps solve this by creating a customisable tool that handles these slightly monotonous tasks, allowing developers to focus on building features instead of project setup.

Building the Package: Key Features

Let me walk you through the main features I've built into this tool:

  • Multiple Database Support: Quickly scaffold your Express.js app with your choice of PostgreSQL, MySQL, SQLite, or MongoDB. No need to worry about different connection setups - the tool handles that for you.
  • Automatic Configuration: Skip the tedious setup phase. The tool creates your .env files, configures your connection strings, and sets up all the necessary folders, getting you ready to code faster.
  • Clean MVC Architecture: Start with a well-organized project structure. Your routes, controllers, and models are neatly arranged following MVC patterns, making your codebase easier to maintain as it grows.
  • TypeScript or JavaScript Support: Whether you prefer the simplicity of JavaScript or the type safety of TypeScript, the choice is yours. The tool generates all the boilerplate in your preferred language.

Choosing a database and language

How I Built It

I built the package using Node.js and utilised the prompts library for interactive prompts, allowing users to select their database and other options. The scaffolding is inspired by how Vite’s script handles templates. I implemented a similar approach by organising different template folders for various frameworks and database setups. Users can choose from options like PostgreSQL, MongoDB, SQLite, or MySQL, each with TypeScript or JavaScript variants.

The CLI functionality is achieved using minimist for argument parsing. File operations, such as copying template files, are handled with fs and custom helper functions. One challenge I faced was understanding how the project structures are output, which I overcame through hands-on experimentation and iteration.

Usage diagram

Usage

To get started, run the command to scaffold your app:

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

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

  1. Enter your project name
  2. Select your database:
    • PostgreSQL
    • MySQL
    • SQLite
    • MongoDB Database options
  3. Choose between JavaScript/TypeScript

Language options

The tool will generate a structure like:

my-project/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ controllers/
β”‚   β”œβ”€β”€ models/
β”‚   β”œβ”€β”€ routes/
β”‚   β”œβ”€β”€ config/
β”‚   β”‚   └── db.js
β”‚   └── app.js
β”œβ”€β”€ .env
└── package.json
Enter fullscreen mode Exit fullscreen mode

Next Steps

The package is still in its early stages, and I plan to expand its capabilities by adding support for more database configurations and authentication boilerplates. In the future, I am also considering exploring alternative server implementations, such as using Go instead of Express, to broaden its use cases and appeal to a wider range of developers.

Conclusion

This project has been an enjoyable and educational journey. I’ve gained valuable insights into building CLI tools and publishing them on npm. I’m eager to continue enhancing the package and making it a useful tool for developers to streamline their project setup. If you’d like to give it a try or contribute, please visit the GitHub repository and share your feedback. Your input would be greatly appreciated!

Your Feedback Matters

Do you have suggestions or ideas for improvement? Are you interested in contributing? I’d be delighted to hear from you and welcome any contributions. You can find the source code here on GitHub.

cli Article's
30 articles in total
Favicon
RAG - Creating the SQLite database and config file
Favicon
Simplifying API Routes in Next.js with next-api-gen
Favicon
Modernizing HyperGraph's CLI: A Journey Towards Better Architecture
Favicon
Making Python CLIs More Maintainable: A Journey with Dynamic Command Loading
Favicon
Making Your CLI Applications Pop with Styled Outputs
Favicon
Makefile
Favicon
Can I start and stop Docker Desktop using CLI?
Favicon
How to Install and Run Redis Directly on macOS (Without Homebrew)
Favicon
From iTerm To WezTerm
Favicon
RAG - Designing the CLI interface
Favicon
Configuring Cisco firewall in Linux machine with Minicom
Favicon
KillPy: The Tool to Clean Up Your Python Virtual Environments 🧹🐍
Favicon
Ubuntu Linux Commands Categorized
Favicon
Rust-Powered Password Decrypter: Find the String Behind the Hash! πŸ¦€πŸ”’
Favicon
Mastering Vim and NvChad for Coding and Development: A Comprehensive Guide
Favicon
The Ultimate Cheat Sheet: CLI Man Pages, tldr, and cheat.sh
Favicon
Git Command Line Mastery: 10 Essential Commands for Version Control Using Git
Favicon
Building My First NPM Package: A CLI for Scaffolding Express Servers
Favicon
.NET Development Essentials on macOS with VS Code
Favicon
How to Configure AWS CLI on Linux
Favicon
Building a GitHub Activity CLI - A Ruby Journey
Favicon
Enhanced CIDR Block Calculator with Expanded Input Formats in Go
Favicon
using cat printer on linux
Favicon
Task Tracker CLI
Favicon
Context Dump: Simplifying AI File Preparation
Favicon
MyTask ToDo CLI Tool...
Favicon
I made wut – a CLI that explains your last command with an LLM
Favicon
Magento 2 commands for maintenance and deployment
Favicon
Rusty Journal: A Minimal and Efficient CLI Journal & ToDo App
Favicon
[Boost]

Featured ones: