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.

npm Article's
30 articles in total
Favicon
NPM command confusion
Favicon
My First npm Package!
Favicon
Introducing date-formatter-i18n: Simplify i18n for Dates in JavaScript
Favicon
Themeify: A Simple Tool to Beautify Your React and Next.js Projects
Favicon
Mi primera Libreria en NPM
Favicon
node unsupported engine when updating npm
Favicon
Starting with semver `1.0.0`
Favicon
My Experience with Node.js Version Compatibility: Leveraging the engines Field in package.json for AutoScout
Favicon
NPM Commands Every Web Developer Must Know
Favicon
Exploring npm: The Node Package Manager
Favicon
When GitHub Actions Build Fails Due to pnpm-lockfile
Favicon
Private npm Repositories
Favicon
πŸš€ Introducing pingflow: Your Ultimate Internet Speed Testing Tool! 🌐
Favicon
npm error 'node' is not recognized as an internal or external command
Favicon
Optimer for your project security and performance issues
Favicon
Publishing NPM package with Github Actions that react-hook-use-cta used
Favicon
Building My First NPM Package: A CLI for Scaffolding Express Servers
Favicon
Resolving Peer Dependency Errors in React: A Comprehensive Guide ⚑
Favicon
Building Scalable Microservices with Node.js and Event-Driven Architecture
Favicon
NPM Dependency error
Favicon
πŸŽ„ A Christmas Gift for Developers: FileToMarkdown!
Favicon
npm
Favicon
Fastly CLI on npm: now at your JavaScript fingertips
Favicon
{ my learnings through Error message β€œerror:0308010C:digital envelope routines::unsupported” }
Favicon
Installing your react.js local package registry to your project
Favicon
External libraries: The Hidden Weight of External Libraries
Favicon
Simplifying your code: replacing heavy packages with lightweight solutions
Favicon
Lazy Devs, Rejoice! Automate Updates with Dependabot (and My Secret Sauce) πŸΉπŸ“±
Favicon
Counter - A React library for animating numeric transitions
Favicon
What I learned building vue3-search-select package

Featured ones: