Recently i decided to run my app with pm2 as a service. For me pm2 start app.mjs runs the app as a service but it seems pm2 doesn't run my express application correctly. I used express npm to build an app like this:
//Imports
import express from "express";
//Setups
const app = express();
const port = process.env.PORT;
app.get("/", async (req, res) => {
res.send("Hello world!");
});
//Running server
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
Also tried pm2 start ./bin/www that gives me an Script not found error and it seems to be okay with express-generator not the express npm alongside an app. App works correctly in dev mode (node app.mjs).
UPDATE
I solved the problem with help of Konstantinos Lamogiannis. After setup the config file as well as the answer, i changed my app.mjs to app.js form and changed the import style to const variable = require(""); and it fixed my problem. I don't know why that happened but it seems the pm2 npm may confilct with new nodejs feature which supports the import ES6 syntax.
pm2 log was giving to me this error: unhandledRejection you may have forgotten to catch a Promise rejection !