DEV Community

Olen Daelhousen
Olen Daelhousen

Posted on

2

fastify-mongodb the ESM way

I just started using ECMAScript (ES) Modules in a new side project I am working on. The stack includes Fastify and MongoDB, so I am using the fastify-mogodb plugin for the database connection. Unfortunately, the documentation for fastify-mongodb did not include an example with ES Modules, so I had to figure it out. Here's how to use fastify-mongodb using ES Modules and import:

server.js

import fastifyMongodb from 'fastify-mongodb';

app.register(fastifyMongodb, {
  forceClose: true,
  useUnifiedTopology: true,
});
Enter fullscreen mode Exit fullscreen mode

As compared to the old-fashioned way using CommonJS:

server.js

fastify.register(require('fastify-mongodb'), {
  forceClose: true,
  useUnifiedTopology: true,
});
Enter fullscreen mode Exit fullscreen mode

Hopefully this will be of use to others, there was not much in the way of solutions when I searched.

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

Best practices for optimal infrastructure performance with Magento

Running a Magento store? Struggling with performance bottlenecks? Join us and get actionable insights and real-world strategies to keep your store fast and reliable.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️