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?

Khushit Shah
  • 546
  • 6
  • 20
Abhilash S
  • 31
  • 1
  • 4
  • 1
    Can you please add your server.js code? Mainly the first few lines containing `require` statements. – Saurav Kumar Nov 27 '21 at 07:56
  • import express from "express"; import http from "http"; const app = express(); const port = 3000; import {getProducts} from './products.mjs'; – Abhilash S Nov 27 '21 at 09:01
  • Check this solution, https://stackoverflow.com/a/64647280/14346235. It might help you. – dinesh oz Nov 27 '21 at 09:47

0 Answers0