For a CommonJS app, use server.cjs:
// server.cjs
const app = require('fastify')({ logger: true })
app.get('/', async () => {
return `Hello ${process.env.HELLO}`
})
app.listen({
port: Number(process.env.PORT || 3000),
host: process.env.HOST || '127.0.0.1'
}).catch((error) => {
app.log.error(error)
process.exit(1)
})
Update the filenames in your scripts:
{
"scripts": {
"dev": "dotenvx run --strict -- node server.cjs",
"start": "dotenvx run -f .env.production --strict -- node server.cjs"
}
}
Run npm run dev or pnpm dev. Dotenvx decrypts .env before Node loads your server. The sharing and production steps stay the same.