0

I am using a vServer for my project, where I run my node backend with pm2. While developing I can open port 80 and 443 on localhost and everything just runs fine. When I uploaded my code to the server the pm2 service instantly crashed. Here is a picutre:

log of pm2

So I looked into my code

import express from 'express';
import fs from 'fs';
import http from 'http';
import https from 'http';
import * as path from 'path';
import {fileURLToPath} from 'url';

const app = express();

const __filename = fileURLToPath(import.meta.url);

const __dirname = path.dirname(__filename);

app.use(express.static(path.resolve(__dirname, 'build')));

app.post('/test1', (req, res) => {
  return res.send('Received a POST HTTP method');
});

app.put('/test2', (req, res) => {
  return res.send('Received a PUT HTTP method');
});

app.delete('/test3', (req, res) => {
  return res.send('Received a DELETE HTTP method');
});

//Start server
http.createServer(app, {
  keepAlive: true
}).listen(80, () => console.info('Listening on port', 80));

//Start https server
https.createServer({
  key: fs.readFileSync('./ssl/privkey.key'),
  cert: fs.readFileSync('./ssl/cert.cer'),
  keepAlive: true
}, app).listen(443, () => console.info('Listening on port', 443));

app.get('*', (req, res) => {
  res.sendFile(path.resolve(__dirname, '../client/build', 'index.html'));
});
index.js

{
  "name": "dashboard-sport",
  "version": "0.1.0",
  "type": "module",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.5",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "dotenv": "^16.0.3",
    "express": "^4.18.2",
    "fs": "^0.0.1-security",
    "http": "^0.0.1-security",
    "https": "^1.0.0",
    "path": "^0.12.7",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-scripts": "5.0.1",
    "url": "^0.11.0",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "proxy": "http://localhost:3000"
}
package.json

My react project and index.js backend are using the same package.json. I added "type": "module" so I can use ES6 imports in both.

I'm deploying with github actions, where the projects gets buid.

Thanks in advance!

Ruffyg
  • 47
  • 1
  • 8
  • You need to `npm install` at server – Saurabh Singh Nov 15 '22 at 16:50
  • This is `package.json` for react app. Where is `package.json` for node? – Konrad Nov 15 '22 at 17:02
  • There is no package.json for node, I'm using the same for both. @KonradLinkowski – Ruffyg Nov 15 '22 at 17:09
  • So how do you start your server? You don't have scripts defined for that – Konrad Nov 15 '22 at 17:10
  • So github actions is deploying for me and makes a new build. I started the backend with "pm2 start index.js --watch" and my backend is loading my frontend as you can see. @KonradLinkowski – Ruffyg Nov 15 '22 at 17:17
  • https://github.com/Unitech/pm2/issues/4540 https://stackoverflow.com/questions/70450332/run-pm2-with-es-modules – Konrad Nov 15 '22 at 17:18
  • Ah thanks, this is really interesting. I tried the part with "npm install pm2@latest -g" and "pm2 update" but nothing changed... same error. Then I tried the part with the ecosystem.config.cjs. So I did "pm2 init simple" and renamed the file to .cjs .... also I changed one line inside, to execute index.js Same error, could cry @KonradLinkowski – Ruffyg Nov 15 '22 at 17:33
  • `pm2 start node -- ndex.js`? – Konrad Nov 15 '22 at 17:34
  • same error, maybe I can't use the package.json in root for my backend... but im clueless right now – Ruffyg Nov 15 '22 at 17:41
  • Also tried my luck with forever right now. Same error, so the problem isn't pm2 – Ruffyg Nov 15 '22 at 18:10

0 Answers0