How to deploy NextJs Project in Server.
2 min readJun 23, 2023
- 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"
},
2. Create server.js
“npm install express
”
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);})
3. pm2 install:
sudo npm i -g pm2
4. Run Project:
npm start
browse:
5.Show pm2 list
pm2 listsudo npm i -g pm2
sudo npm install pm2 -gpm2 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
Another way(Using Build):
- Edit the Next.js configuration file:
// next.config.jsmodule.exports = {
reactStrictMode: true,
eslint: {
ignoreDuringBuilds: true,
},
};
2. then build:
npx next build
npx next export