Micro — Asynchronous HTTP Microservices
Micro is a minimalistic library for creating ultra-lightweight, asynchronous HTTP microservices. It's built around the `async`/`await` pattern, enabling developers to write concise and highly performant request handlers with minimal boilerplate. The current stable version is 10.0.1, which introduced ES Modules support and TypeScript conversion. While it doesn't adhere to a fixed release cadence, updates are released to maintain compatibility with newer Node.js versions and modern JavaScript features. Its core differentiators include a tiny codebase (approximately 260 lines of code), explicit dependency management without implicit middleware, and a design philosophy focused on standard HTTP interactions. It is specifically intended for use within containerized environments, and the project explicitly advises against its use in serverless platforms like Vercel, where platform-native helpers often provide similar or superior functionality.
Common errors
-
Error: Cannot find module 'micro' or its corresponding type declarations.
cause Incorrect import path or missing type definitions for TypeScript.fixEnsure `micro` is installed and update your `tsconfig.json` to include `node_modules/@types` or use the correct `import` statement. For CommonJS, use `require('micro')`. -
Error [ERR_REQUIRE_ESM]: require() of ES Module ... micro/index.js from ... not supported.
cause Attempting to `require()` an ES Module in a CommonJS context, often occurring after upgrading to `micro` v10 without fully migrating to ESM.fixIf your project is CommonJS, stick to `module.exports` for your handler. If using `import` for helpers, ensure your `package.json` has `"type": "module"` and your files use `.mjs` or are transpiled correctly. -
Error: Port 3000 is already in use.
cause Another process is already listening on the default Micro port (3000) or a port specified in your configuration.fixEither stop the conflicting process or specify a different port for Micro using `micro -l tcp://0.0.0.0:XXXX` where XXXX is an unused port. -
unknown option `--port`
cause Using the deprecated `--port`, `--host`, or `--unix-socket` command-line arguments from `micro` versions prior to v10.fixUpdate your `start` scripts or CLI commands to use the `--listen (-l)` option, e.g., `"start": "micro -l tcp://0.0.0.0:3000"`.
Warnings
- breaking Version 10.0.0 drops support for Node.js versions older than 16.0.0. Projects using older Node.js runtimes will fail to start or install.
- breaking The command-line arguments `--port (-p)`, `--host (-h)`, and `--unix-socket (-s)` were removed in v10.0.0. Use the `--listen (-l)` option for specifying endpoint URIs.
- gotcha Micro is explicitly designed for use in containerized environments and is not recommended for serverless platforms like Vercel. Vercel's built-in Serverless Function helpers often provide superior or equivalent functionality without Micro's overhead.
- gotcha For local development, it is strongly recommended to use `micro-dev` instead of `micro`. `micro-dev` provides features like file watching, automatic restarts, and better error reporting tailored for a development workflow.
- breaking Version 10.0.0 converted `micro` to TypeScript and added ES Modules support. This might affect how you import `micro` or its helpers, especially in mixed CJS/ESM projects or older tooling.
Install
-
npm install micro -
yarn add micro -
pnpm add micro
Imports
- micro
import { micro } from 'micro';import micro from 'micro';
- send
import micro, { send } from 'micro';import { send } from 'micro'; - json
const json = micro.json;
import { json } from 'micro'; - handler function
module.exports = async function handler(req, res) { /* ... */ };export default async (req: IncomingMessage, res: ServerResponse) => { /* ... */ };
Quickstart
import { IncomingMessage, ServerResponse } from 'http';
import { json, send } from 'micro';
interface MyRequestBody {
name: string;
}
export default async (req: IncomingMessage, res: ServerResponse) => {
if (req.method === 'POST') {
const data = (await json(req)) as MyRequestBody;
return send(res, 200, { message: `Hello, ${data.name}!` });
} else {
return 'Welcome to Micro! Send a POST request with JSON { "name": "YourName" }.';
}
};