Question

502 bad gateaway nginx

Posted June 1, 2021 257 views
NginxNode.js

Hello, i have been getting bad gateaway error in my MERN application. with pm2 logs, i have these errors:

/root/.pm2/logs/server-error.log last 15 lines:
0|server   |     at Module.load (internal/modules/cjs/loader.js:928:32)
0|server   |     at Function.Module._load (internal/modules/cjs/loader.js:769:14)
0|server   |     at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
0|server   |     at internal/main/run_main_module.js:17:47
0|server   | Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /root/GottaGetGamesWeb/backend/server.js
0|server   |     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1080:13)
0|server   |     at Module.load (internal/modules/cjs/loader.js:928:32)
0|server   |     at Function.Module._load (internal/modules/cjs/loader.js:769:14)
0|server   |     at Object.<anonymous> (/root/.nvm/versions/node/v14.15.5/lib/node_modules/pm2/lib/ProcessContainerFork.js:48:21)
0|server   |     at Module._compile (internal/modules/cjs/loader.js:1063:30)
0|server   |     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
0|server   |     at Module.load (internal/modules/cjs/loader.js:928:32)
0|server   |     at Function.Module._load (internal/modules/cjs/loader.js:769:14)
0|server   |     at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
0|server   |     at internal/main/run_main_module.js:17:47

please help me.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi there @heysuneel,

It sounds like that your Node.js version might be outdated. You can check this with the following command:

node -v

Starting from Node.js version 12.14.1, you should not get that error.

You could use NVM to install specific Node.js versions:

https://www.digitalocean.com/community/tutorials/nodejs-node-version-manager

Let me know how it goes.
Regards,
Bobby

by joshtronic
The Node Version Manager is a great tool to use, allowing you to install multiple versions of Node.js and switch between them as you see fit. In this tutorial, you will install `nvm` and learn to install, remove, and switch between different versions of Node.js.
  • Hello @bobbyiliev , thank you for your reply. I also watched your youtube video. I checked the node version and the version is v14.15.5

    • Hi there @heysuneel,

      Ah that is nice! The video is mainly explaining why the 502 error occurs. In your case it is the backend service that is having problems and the Nginx service seems to be configured correctly.

      I came the same Node.js problem being discussed here

      The main suggestion is to make sure your package.json includes a line for "type": "module". Without this line, node assumes you want to use Common JS modules rather than ESM.

      Let me know how this goes!
      Regards,
      Bobby

      • @bobbyiliev i included that line. Re installed packages but still the same error :(
        what do i do

        • Hello,

          Does it work locally on your dev environment? If so, what is the exact Node version that you have installed there?

          Regards,
          Bobby

          • It works locally. The version is 14.5.1

            Also in server, if i run the server through node backend/server.js , it works.

            Regards,
            Suneel

          • Hi there @heysuneel,

            I’ve taken a closer look into your log, it looks like that you are already using NVM. I think that the problem could be that the pm2 is using a different Node version than the one that you’ve specified with nvm which would explain the error in case that pm2 is using an older Node version binary.

            What you could try is to pass the path to the Node v14.15.5 binary when you run the pm2 command:

            pm2 start app.js --interpreter=/root/.nvm/versions/node/v14.15.5/bin/node
            

            Let me know how this goes!

            Regards,
            Bobby

          • @bobbyiliev thank you it worked. I instead updated the pm2 to its latest version.

          • Hi there @heysuneel,

            No problem at all! Happy to hear that you’ve got it all working!

            Regards,
            Bobby