DEV Community

MD.Younus Islam
MD.Younus Islam

Posted on

Stop Writing Repetitive try/catch in Express πŸš€ β€” Meet express-catchify

express-catchify code-block photo

🧩 What is express-catchify?

If you’ve ever written this πŸ‘‡ repeatedly:

app.get("/", async (req, res, next) => {
  try {
    const data = await getData();
    res.json(data);
  } catch (err) {
    next(err);
  }
});
Enter fullscreen mode Exit fullscreen mode

You know how boring and repetitive try/catch blocks can get.

So, I built express-catchify β€” a lightweight (less than 1KB!) helper that automatically catches async errors in your Express routes.

⚑ Why I built it

While learning Express, I noticed almost every project had the same pattern:

  • A bunch of try/catch blocks everywhere
  • Lots of repetitive next(err) calls
  • Middleware getting messy

I wanted a cleaner, reusable, modern way to wrap async routes.
Something you just plug in and forget about.

πŸš€ How to Install

Using npm:

npm install express-catchify
Enter fullscreen mode Exit fullscreen mode

Or yarn:

yarn add express-catchify
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ How to Use

ES Module

import express from "express";
import catchify from "express-catchify";

const app = express();

app.get("/", catchify(async (req, res) => {
  const data = await getData();
  res.json(data);
}));

app.use((err, req, res, next) => {
  res.status(500).json({ message: err.message });
});
Enter fullscreen mode Exit fullscreen mode

CommonJS

const express = require("express");
const catchify = require("express-catchify").default;

const app = express();

app.get("/", catchify(async (req, res) => {
  const data = await getData();
  res.json(data);
}));

app.use((err, req, res, next) => {
  res.status(500).json({ message: err.message });
});
Enter fullscreen mode Exit fullscreen mode

πŸ§ͺ Why it’s Useful

βœ… Removes repetitive try/catch blocks
βœ… Makes route handlers clean and readable
βœ… Works with any async middleware
βœ… 100% tested with Vitest

βœ… MIT licensed and open source

πŸ“¦ Check it Out

πŸ“˜ GitHub β†’ github express-catchify
πŸ“¦ npm β†’ npm express-catchify

πŸ’¬ Feedback

I’d love to hear your thoughts:

  • Would you use something like this in a production environment?
  • Do you have any suggestions for improving the developer experience (DX)?
  • Should I make a TypeScript-first version?

Top comments (0)