I have package.json scripts as follows:
"scripts": {
"build": "NODE_ENV=production next build",
"startLocal": "NODE_ENV=production node server.js",
"startServer": "NODE_ENV=production pm2 start server.js",
}
Now, my app uses NextJS with a custom Express server. server.js looks a bit like this:
const { parse } = require('url');
const express = require('express');
const next = require('next');
const env = process.env.NODE_ENV || 'development';
const port = parseInt(process.env.PORT, 10) || 3000;
const app = next({ dev: env === 'development' });
const handle = app.getRequestHandler();
app.prepare().then(() => {
const server = express();
server.get('*', (req, res) => {
handle(req, res, parse(req.url, true));
});
server.listen(port, (err) => {
if (err) throw err;
console.info(`> Ready on port ${port}`);
});
});
To build my app, I first run npm run build
. Then I launch the app on a specific port number PORT=3030 npm run startLocal
. This works as expected. However, when I use the script for a PM2 launch PORT=3030 npm run startServer
it doesn't pick up the port number and launches on port 3000.
Why is this? Is there something else I need to add to the PM2 command in my package.json script?
pm2仅了解环境变量NODE_PORT(not PORT),因此我认为您需要使用PORT=3030 NODE_PORT=3030 npm run startServer
以便将其传递给next和pm2。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.