DEV Community

Olen Daelhousen
Olen Daelhousen

Posted on

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)