0

My NodeJS app is working perfect with command

# node server.js 

but when starting with pm2

# pm2 start server.js 

it immediately stopping with error in logs as follows

0|server   |     at internal/main/run_main_module.js:17:47 {
0|server   |   code: 'ERR_REQUIRE_ESM'
0|server   | }
0|server   | Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /var/www/html/server.mjs
0|server   |     at Module.load (internal/modules/cjs/loader.js:861:11)
0|server   |     at Function.Module._load (internal/modules/cjs/loader.js:708:14)
0|server   |     at Object.<anonymous> (/usr/lib/node_modules/pm2/lib/ProcessContainerFork.js:33:23)
0|server   |     at Module._compile (internal/modules/cjs/loader.js:999:30)
0|server   |     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
0|server   |     at Module.load (internal/modules/cjs/loader.js:863:32)
0|server   |     at Function.Module._load (internal/modules/cjs/loader.js:708:14)
0|server   |     at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
0|server   |     at internal/main/run_main_module.js:17:47 {
0|server   |   code: 'ERR_REQUIRE_ESM'
0|server   | }

I created package.json and the file is there , what else could be the issue?

3
  • 1
    Can you please add your server.js code? Mainly the first few lines containing require statements. Commented Nov 27, 2021 at 7:56
  • import express from "express"; import http from "http"; const app = express(); const port = 3000; import {getProducts} from './products.mjs'; Commented Nov 27, 2021 at 9:01
  • Check this solution, stackoverflow.com/a/64647280/14346235. It might help you. Commented Nov 27, 2021 at 9:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.