Avvio: Asynchronous Application Bootstrapping

9.2.0 · active · verified Sun Apr 19

Avvio is a robust, reentrant, and graph-based library designed for managing the asynchronous bootstrapping of Node.js applications. It simplifies complex application startup sequences by handling plugin loading order, inter-plugin dependencies, and comprehensive error management automatically. Unlike simpler sequential loaders, Avvio allows plugins to register other plugins, ensuring correct execution flow within deeply nested structures. The current stable version is 9.2.0, with a release cadence of minor and patch updates every few weeks or months, often driven by dependency updates or minor feature enhancements. Its key differentiator is its emphasis on reentrancy and a dependency graph, which guarantees that even deeply nested plugins are initialized in the precise order required, coupled with sophisticated error handling capabilities.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to initialize Avvio, register a series of asynchronous plugins including nested ones and those leveraging async/await syntax, and then wait for the application to be fully booted before gracefully closing it.

'use strict'

const avvio = require('avvio')
const app = avvio()

app
  .use(first, { hello: 'world' })
  .after((err, cb) => {
    if (err) {
      console.error('Error in after hook:', err);
      return cb(err);
    }
    console.log('after first and second')
    cb()
  })

app.use(third)

app.ready(function (err) {
  // the error must be handled somehow
  if (err) {
    throw err
  }
  console.log('application booted!')
  // In a real app, you might start listening for requests here
  // For demonstration, we'll close the app immediately
  app.close().then(() => console.log('application closed.')).catch(e => console.error('Error closing:', e));
})

function first (instance, opts, cb) {
  console.log('first loaded', opts)
  instance.use(second)
  cb()
}

function second (instance, opts, cb) {
  console.log('second loaded')
  process.nextTick(cb)
}

// async/await or Promise support
async function third (instance, opts) {
  console.log('third loaded')
  // Simulating async work
  await new Promise(resolve => setTimeout(resolve, 50));
}

view raw JSON →