{"id":18337,"library":"express-init","title":"express-init","description":"Express middleware initialization utility that allows middleware to perform asynchronous setup before the server starts. Version 1.1.2 supports Express 4.x. It calls a user-defined `init` function on each middleware serially before invoking a final callback. Unlike other async setup patterns (e.g., Promises), it uses callbacks and is tightly coupled to Express middleware structure. Maintenance is low; last release was several years ago. Differentiating factor: lightweight, no additional dependencies, and middleware-based initialization pattern.","status":"maintenance","version":"1.1.2","language":"javascript","source_language":"en","source_url":"https://github.com/activeprospect/express-init","tags":["javascript"],"install":[{"cmd":"npm install express-init","lang":"bash","label":"npm"},{"cmd":"yarn add express-init","lang":"bash","label":"yarn"},{"cmd":"pnpm add express-init","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is CJS-only and does not support ES modules. Use require() only.","wrong":"import initialize from 'express-init';","symbol":"default","correct":"const initialize = require('express-init');"},{"note":"In TypeScript with CJS modules, use `import = require()` syntax.","wrong":"import initialize from 'express-init';","symbol":"default (TypeScript)","correct":"import initialize = require('express-init');"},{"note":"The module exports a single function directly; do not destructure or access .init property.","wrong":"var init = require('express-init').init;","symbol":"initialize","correct":"const init = require('express-init');\ninit(app, callback);"}],"quickstart":{"code":"const express = require('express');\nconst initialize = require('express-init');\n\nconst app = express();\n\nconst myMiddleware = (req, res, next) => {\n  console.log('Request received');\n  next();\n};\nmyMiddleware.init = (app, callback) => {\n  // Simulate async init (e.g., DB connection)\n  setTimeout(() => {\n    console.log('Middleware initialized');\n    callback();\n  }, 100);\n};\n\napp.use(myMiddleware);\napp.get('/', (req, res) => res.send('Hello'));\n\ninitialize(app, (err) => {\n  if (err) throw err;\n  app.listen(3000, () => console.log('Server started'));\n});","lang":"javascript","description":"Demonstrates defining an Express middleware with an init function and using express-init to initialize all middleware before starting the server."},"warnings":[{"fix":"Ensure your Express version is 4.x.","message":"Only supports Express 4.x. Not compatible with Express 3.x or 5.x.","severity":"breaking","affected_versions":">=0.0.0"},{"fix":"Consider wrapping initialization in a Promise or using alternative libraries that support async/await.","message":"The package uses callback-based asynchronous pattern. Modern codebases prefer Promises or async/await.","severity":"deprecated","affected_versions":">=0.0.0"},{"fix":"Ensure that your init function does not rely on being called multiple times.","message":"Middleware init functions are called only once, even if the middleware is used multiple times in the same app.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Always call callback with an error if initialization fails; do not throw.","message":"The library does not handle errors from init functions gracefully; throwing in init will crash the process.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Ensure your middleware explicitly defines an `init` method if async initialization is needed.","message":"Works only with middleware that have an `init` function attached. Middleware without init are skipped silently.","severity":"gotcha","affected_versions":">=0.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Use `const initialize = require('express-init');` instead of `import initialize from 'express-init';`","cause":"Using ES module import syntax on a CJS-only module.","error":"initialize is not a function"},{"fix":"Add an `init` function to your middleware: `middleware.init = function(app, callback) { ... }`","cause":"Middleware attached to app does not have an `init` function defined.","error":"TypeError: middleware.init is not a function"},{"fix":"Call `initialize(app, function(err) { ... })` with a valid callback.","cause":"The second argument to initialize() is missing or not a function.","error":"Callback must be a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}