Learn how to prepare a Node.js application for production, configure environment variables, start a server, and deploy your application to a hosting platform.
Deployment is the process of making an application available for users to access. During development, you usually run your application on your own computer. After deployment, users can access it through a public URL or domain.
http://localhost:3000
Usually used by developers to build and test an application on their own computer.
https://your-domain.com
A deployed application can be accessed by users over the internet.
For this lesson, create a simple Express application.
mkdir node-deployment-demo
cd node-deployment-demo
npm init -y
npm install express
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
app.get('/', (req, res) => {
res.send('Welcome to Soopro Pathshala!');
});
app.get('/about', (req, res) => {
res.json({
application: 'Node.js Deployment Demo',
message: 'Application is running successfully'
});
});
app.get('/health', (req, res) => {
res.status(200).json({
status: 'ok'
});
});
app.listen(PORT, '0.0.0.0', () => {
console.log(`Server is running on port ${PORT}`);
});
process.env.PORT. Do not assume production
applications will always use port 3000.
The hosting platform needs to know how to start your application.
Add a start script to the scripts section of
package.json.
{
"name": "node-deployment-demo",
"version": "1.0.0",
"description": "Node.js deployment example",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^5.0.0"
}
}
Run the application using the start command.
npm start
Open these URLs in your browser:
http://localhost:3000/http://localhost:3000/abouthttp://localhost:3000/healthFor the home page:
Welcome to Soopro Pathshala!
For the health endpoint:
{
"status": "ok"
}
| Development | Production |
|---|---|
| Used while building the application. | Used by real users. |
| Often runs on localhost. | Runs on a server or hosting platform. |
| May use test data. | Must protect real user data. |
| Debugging output can be more detailed. | Errors must not expose sensitive information. |
| Local configuration may be used. | Production configuration and secrets are required. |
Production applications should receive their configuration from the hosting environment. Avoid hardcoding database passwords, API keys, and JWT secrets.
const PORT = process.env.PORT || 3000;
const NODE_ENV = process.env.NODE_ENV || 'development';
console.log(`Environment: ${NODE_ENV}`);
On your hosting platform, configure the required environment variables through its settings or secret-management system.
Many Node.js hosting providers let you connect a Git repository, install dependencies, and run a start command. The exact interface varies by provider.
npm start.| Runtime | Node.js |
|---|---|
| Install / Build Command | npm install (if required by the platform) |
| Start Command | npm start |
| Port | Use the platform-provided PORT variable. |
These are common settings, not a guarantee that every hosting provider uses the same configuration.
A virtual private server (VPS) gives you more control over the server. You are generally responsible for installing Node.js, configuring the application process, securing the server, and setting up HTTPS.
npm ci --omit=dev
Run this command in the application directory when a compatible package-lock.json file is available. It installs the locked production dependencies without development dependencies.
A process manager can keep an application running and help restart it after a crash or server reboot, depending on its configuration. PM2 is one commonly used Node.js process manager.
npm install -g pm2
pm2 start server.js --name node-demo
pm2 list
pm2 logs node-demo
pm2 restart node-demo
PM2 is an optional tool. Many managed hosting platforms already handle application processes for you.
| Error | Possible Solution |
|---|---|
| Application fails to start. | Check the start command and deployment logs. |
| Cannot find module. | Check package.json and install required dependencies. |
| Port binding error. | Use the port assigned by the hosting platform. |
| Environment variable is undefined. | Check its name and whether it is configured in the hosting settings. |
| Database connection fails. | Verify credentials, network access, database availability, and configuration. |
| Application works locally but not online. | Check runtime versions, filesystem paths, environment variables, and logs. |
process.env.PORT.npm start to run the configured start script.process.env.PORT for the hosting platform's port.Choose the correct answer for each question.
1. What does deployment mean?
2. Which command runs the start script in package.json?
3. How should a production application receive its secret credentials?