Integration with Node.js
Elysia provide a runtime adapter to run Elysia on multiple runtime, 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 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 recommended installing tsx or ts-node with nodemon.
tsx is a CLI that transpiles TypeScript to JavaScript with hot-reload and several more feature you expected 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 change.
- build - Build the application for production usage.
- start - Start an Elysia production server.
Make sure to create tsconfig.json
tsc --initDon't forget to update tsconfig.json to include 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