Integration with Node.js
Elysia provides a runtime adapter to run Elysia on multiple runtimes, including Node.js.
To run Elysia v2 on Node.js, install the v2 Node adapter. The v1 adapter is a different release line.
bun add elysia@rc @elysia/node@rcpnpm add elysia@rc @elysia/node@rcnpm install elysia@rc @elysia/node@rcyarn add elysia@rc @elysia/node@rcThen apply the Node adapter to your main Elysia instance.
import { Elysia } from 'elysia'import { node } from '@elysia/node'
const app = new Elysia({ adapter: node() }) .get('/', () => 'Hello Elysia') .listen(3000)This is all you need to run Elysia on Node.js.
Additional setup (optional)
Section titled “Additional setup (optional)”For the best experience, we recommend installing tsx.
tsx is a CLI that transpiles TypeScript to JavaScript with hot-reload and several more features you would expect from a modern development environment.
bun add -d tsx @types/node typescriptpnpm add -D tsx @types/node typescriptnpm install --save-dev tsx @types/node typescriptyarn add -D tsx @types/node typescriptThen open your package.json file, set the package to use ES modules, and add the following scripts:
{ "type": "module", "scripts": { "dev": "tsx watch src/index.ts", "build": "tsc -p tsconfig.json", "start": "NODE_ENV=production node dist/index.js" }}These scripts refer to the different stages of developing an application:
- dev - Start Elysia in development mode with auto-reload on code changes.
- build - Build the application for production use.
- start - Start a production Elysia server.
Make sure to create a tsconfig.json:
tsc --initThen configure these options in tsconfig.json:
{ "compilerOptions": { "target": "ES2022", "module": "NodeNext", "moduleResolution": "NodeNext", "lib": ["ESNext", "DOM", "DOM.Iterable"], "types": ["node"], "rootDir": "src", "outDir": "dist", "strict": true, "skipLibCheck": true }, "include": ["src/**/*.ts"]}The build script reads this configuration, including strict and the output directory. Use .js extensions when importing your own modules with NodeNext. tsx watch reloads on code changes for a similar experience to bun dev.
If your pnpm configuration does not install peer dependencies, add the peers required by Elysia v2 explicitly:
pnpm add typebox exact-mirror openapi-types