dev-resources.site
for different kinds of informations.
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.
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
To get started, run the command to scaffold your app:
npx create-mvc-server
After running the command, you'll be prompted to:
- Enter your project name
- Select your database:
- PostgreSQL
- MySQL
- SQLite
- MongoDB
- Choose between JavaScript/TypeScript
The tool will generate a structure like:
my-project/
βββ src/
β βββ controllers/
β βββ models/
β βββ routes/
β βββ config/
β β βββ db.js
β βββ app.js
βββ .env
βββ package.json
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.
Featured ones: