npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

fastify-esm-loader

v0.0.11

Published

An esm-based loader for your Fastify applications.

Readme

fastify-esm-loader

An esm-based loader for your Fastify applications.

npm i fastify-esm-loader --save

Features

  • Automatically discovers route handlers from file system
  • Introduces handy idioms and helpers for dependency injection
  • Forces a clean, well organized style for route handler definitions

Example

npm i
cd example/
node index.js

Usage

import { join } from 'path'
import Fastify from 'fastify'
import FastifyESMLoader from 'fastify-esm-loader'

const fastify = Fastify({
  logger: {
    prettyPrint: {
      levelFirst: true,
    },
  },
})

fastify.register(FastifyESMLoader, {
  baseDir: join(__dirname, 'routes'),
  injections: {
    someRootHelper () {
      return 'foobar'
    }
  }
})

You can also use injections to make available things like db and redis.

Check examples/index.js and examples/main.js for the full boilerplate.

Assuming baseDir is routes, fastify-esm-loader will recursively look for index.js files use them to register routes, at any depth. Take for example project/routes/users/index.js -- here users is a route group -- you can have multiple route groups under baseDir. This file must export a function with a signature like this:

export default function ({ fastify, self, env }) {
  fastify.get('/users/all', self.listUsers)
  fastify.get('/users/:id', self.getUser)
  fastify.post('/users/', self.createUser)
}

For that to work, you'd have to have three files under routes/users:

project/routes/users/listUser.js
project/routes/users/createUser.js
project/routes/users/createUser.js

And each of these files, export a default function that is then used as a route handler. The loader will make them available in self in the main route definition function (users/index.js), so that you can easily tweak the mapping.

The mechanics described above will work for both top-level files and subfolders.

That means the following setup work would the same:

project/routes/index.js
project/routes/topLevelMethod.js
project/routes/users/listUser.js
project/routes/users/createUser.js
project/routes/users/createUser.js

Handlers

There are two ways to define a handler:

  1. A function that returns the handler, useful for injections (and environment access):
export default ({ env }) => {
  return async (request, reply) => {
    reply.send(await auth.authenticate(env.JWT_TOKEN))
  }
}
  1. A direct handler export:
export default async function (request, reply) {
  reply.send({ message: 'No injections needed here' })
}

Environment shorthand

NODE_ENV is used to populate env.$node_env (lowercase, prefixed with $).

This allows for checks such as this:

export default ({ env, fastify, self }) => {
  if (env.$staging) {
    fastify.get('/staging-only', self.stagingOnly)
  }
}

License

MIT