How to deploy NextJs Project in Server.

  1. Edit package.json
"scripts": {
"dev": "node server.js",
"prod": "node server.js",
"build": "next build && next export",
"start": "NODE_ENV=production pm2 start server.js --watch",
"export": "npm run build && next export"
},
const express = require('express');
const next = require('next');
const port = 3661;

const dev = process.env.NODE_ENV !== 'production';
const app = next({dev});
const handle = app.getRequestHandler();

app.prepare().then(() => {
const server = express();
server.all('*', (req, res) => {
return handle(req, res);
});
server.listen(port, err => {
if (err) throw err;
console.log(`ready on http://localhost:${port}`)
})
}).catch(ex => {
console.error(ex.stack);
process.exit(1);

})
sudo npm i -g pm2
npm start
npm start
pm2 listsudo npm i -g pm2
sudo npm install pm2 -g
pm2 list //list all//root
pm2 stop server.js --watch //pmt start
pm2 stop server.js --watch //pmt stop
pm2 start server.js --name "pm2_name" //pm2 start
//root
npm start
pm2 list
pm2 monitor
pm2 unmonitor
pm2 restart
# Display all apps logs in realtime
pm2 logs

# Display only `api` application logs
pm2 logs api

# Display new logs in json
pm2 logs --json

# Display 1000 lines of api log file
pm2 logs big-api --lines 1000
#You can also check logs with the CLI dashboard:
pm2 monit

--

--

Senior Software Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store