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 🙏

© 2024 – Pkg Stats / Ryan Hefner

fastify-esm-autoload

v1.2.2

Published

<span style="font-size: 1.5em;">**NOTE** The [fastify-autoload](https://github.com/fastify/fastify-autoload) plugin automatically works with ESM in Node as of [Version 3](https://github.com/fastify/fastify-autoload/tree/v3.0.0). If you are using this modu

Downloads

4

Readme

Fastify Autoload ES Modules

NOTE The fastify-autoload plugin automatically works with ESM in Node as of Version 3. If you are using this module and upgrade to Fastify 3, replace this module with the official Fastify module.

I wrote this because I want to be able to use ES Modules (ESM) with Fastify and Node 12+. It has been tested to work in a Node 14.x environment as of 2020-05-01. Once Node 12.x is no longer in LTS, and if the default Fastify Autoload module has not yet added in this functionality, I shall remove any --experimental-modules flags from the project.

This is not meant to be an ESM Compatible fork of fastify-autoload, as I don't have a lot of free time to map all of the use cases out.

Things this Fastify plugin does:

  1. Allows you to have an autoloader with ESM

Things this plugin does not do:

  1. Work with TypeScript -- at least in my quick and dirty tests of it. Perhaps I'll add the ability in once I learn more about the interactions of TypeScript with ESM in a Node environment.

Installation

yarn add fastify-esm-autoload
# or npm install --save fastify-esm-autoload

Usage

You must use this from within a Node ESM module. This means either using the .mjs extension, or setting "type":"module" on the top level of your package.json file.

// server.js
import { resolve } from 'path';

import Fastify from 'fastify';
import Autoload from 'fastify-esm-autoload';

const fastify = Fastify();

fastify.register(Autload, {
  dir: resolve(process.cwd(), 'routes');
});

fastify.listen(3000, (err)=> {
  if(err) {
    console.error(err);
    process.exit();
  }
  console.log('Listening')
});

// routes/main.js
export default async (fastify) => {
  fastify.get('/', async () => {
    return 'working!';
  });
  return true;
}

Then, in your console run

node --experimental-modules server.js

NOTE

In Node's ESM there is not the __dirname constant, so if your code relies on that, a simple fix is to do the following:

import { resolve } from 'path';
const __dirname = resolve('.');
// or you could just use this code directly.

fastify.register(Autoload, {
  dir: resolve('.','routes')
})

Because you may want to pass options to a specific group of autoloaded plugins, you can do so like this:

// server.js
import {resolve} from 'path';

import Fastify from 'fastify';
import Autoload from 'fastify-esm-autoload';

const fastify = Fastify();

fastify.register(Autoload, {
  dir: resolve(process.cwd(), 'grouped'),
  options: {
    something: 'some value '
  }
});

Auto Prefix

If you would like to apply a prefix to all routes in a file, you can do this by exporting a constant called autoPrefix.

//server.js
import {resolve} from 'path';
import Fastify from 'fastify';
import Autoload from 'fastify-esm-autoload';
const fastify = Fastify();

fastify.register(Autoload, {
  dir: resolve(process.cwd(), 'routes')
});

//routes/posts.js
export default async (fastify) => {

  // will be registered under /posts
  fastify.get('/', async () => 'Post Index');
  // will be registered under /posts/:id
  fastify.get('/:id', async (req) => 'Post with ID of '+ req.params.id);
  // other routes...
  return true;
};
export const autoPrefix = '/posts';

NOTE My auto prefix is not the same as the official Fastify Autoload. My auto prefix implementation will override any prefix given in the options object when the plugin is loaded. For example:

// same imports as above example
fastify.register(Autoload, {
  dir: resolve(process.cwd(), 'routes'),
  prefix: '/defaultPrefix'
});

// routes/user.js
export default async (fastify) => {
  // Will NOT be registered at /defaultPrefix/user
  // WILL be registered at /user
  fastify.get('/', async () => 'Routes/User')
  return true;
}

export const autoPrefix = '/user';

Auto Prefixing

For the given file structure

If we do the following

fastify.register(Autoload, {
  dir: resolve(process.cwd(), 'routes');
});

The routes added in file1.js and file2.js will have path1/ prefixed automatically.

Similarly, the routes in file3.js and file4.js will have their routes prefixed with path2.

NOTE This only goes one level deep. It will not work with any folders inside of path1/ or path2/

CommonJS Interoperability

The plugin has been tested to work with .js files in a folder with {"type":"commonjs"} using module.exports = ... in place of export default ..., so if for some reason you want to use this plugin within a CJS environment, you shouldn't run into any errors.

Type Definitions

This package comes with it's own TypeScript definitions; no @types/ repo necessary!