Integration with Node.js
Elysia provides a runtime adapter to run Elysia on multiple runtimes, including Node.js.
To run Elysia on Node.js, simply install Node adapter.
bun add elysia @elysiajs/nodepnpm add elysia @elysiajs/nodenpm install elysia @elysiajs/nodeyarn add elysia @elysiajs/nodeThen apply the Node adapter to your main Elysia instance.
import { Elysia } from 'elysia'
import { node } from '@elysiajs/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)
For the best experience, we recommend installing tsx or ts-node with nodemon.
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 and add the following scripts:
{
"scripts": {
"dev": "tsx watch src/index.ts",
"build": "tsc src/index.ts --outDir dist",
"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 --initDon't forget to update tsconfig.json to set compilerOptions.strict to true:
{
"compilerOptions": {
"strict": true
}
}This will give the hot reload, JSX support to run Elysia with the similar experience as bun dev
pnpm
If you use pnpm, pnpm doesn't auto install peer dependencies by default forcing you to install additional dependencies manually.
pnpm add @sinclair/typebox openapi-types