Deploy a Node.js application
Set up environment variables, the startup script and deploy via Git to your instance.
Deploy a Node.js application
Deploying a Node.js application on your onesubnet.com instance comes down to three steps: pull the code, configure environment variables, and start the process.
Cause / The problem
An application that starts and then crashes, or that cannot find its configuration variables, usually suffers from a missing .env file, an incorrect startup script, or dependencies that are not installed.
Solution
- Connect to your instance via SSH or the panel terminal.
- Pull the code from Git:
Or upload your files via SFTP into the application folder.git clone https://github.com/your-org/your-app.git cd your-app - Install dependencies:
npm install # or: bun install / yarn install - Create the
.envfile at the project root with your environment variables:
Never commit this file to Git (addNODE_ENV=production PORT=3000 DATABASE_URL=postgresql://user:password@db-paris.onesubnet.com:5432/mydb JWT_SECRET=your_long_random_secret.envto.gitignore). - Start the application directly to verify it boots:
npm start # or: node index.js / node dist/main.js - Put the application into production with PM2 so it runs in the background and restarts automatically:
npm install -g pm2 pm2 start npm --name "my-app" -- start pm2 save pm2 startup # enables startup on system boot - For later updates via Git:
git pull origin main npm install pm2 restart my-app - Check the logs if a crash occurs:
pm2 logs my-app --lines 50
For a clean setup, create a PM2 ecosystem.config.js file that defines env and env_production, then start with pm2 start ecosystem.config.js --env production.