Untitled Page

ID: 2290https://elysiajs.com/integrations/node.md
Source

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.

::: code-group

bun add elysia @elysiajs/node
pnpm add elysia @elysiajs/node
npm install elysia @elysiajs/node
yarn add elysia @elysiajs/node

:::

Then apply node adapter to your main Elysia instance.

import { Elysia } from 'elysia'
import { node } from '@elysiajs/node' // [!code ++]

const app = new Elysia({ adapter: node() }) // [!code ++]
	.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.

::: code-group

bun add -d tsx @types/node typescript
pnpm add -D tsx @types/node typescript
npm install --save-dev tsx @types/node typescript
yarn add -D tsx @types/node typescript

:::

Then 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 --init

Don'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