'pm2 can't find package.json when start

I try to start my app on VPS.I can start my app in terminal, but when I work with pm2 , I get the error

0|gentle-s | Error: Cannot find module '/root/my-app/index.js'. Please verify that the package.json has a valid "main" entry
0|gentle-s |     at tryPackage (node:internal/modules/cjs/loader:353:19)
0|gentle-s |     at Function.Module._findPath (node:internal/modules/cjs/loader:566:18)
0|gentle-s |     at Function.Module._resolveFilename (node:internal/modules/cjs/loader:919:27)
0|gentle-s |     at Function.Module._load (node:internal/modules/cjs/loader:778:27)
0|gentle-s |     at Object.<anonymous> (/root/.nvm/versions/node/v16.13.2/lib/node_modules/pm2/lib/ProcessContainerFork.js:33:23)
0|gentle-s |     at Module._compile (node:internal/modules/cjs/loader:1101:14)
0|gentle-s |     at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
0|gentle-s |     at Module.load (node:internal/modules/cjs/loader:981:32)
0|gentle-s |     at Function.Module._load (node:internal/modules/cjs/loader:822:12)
0|gentle-s |     at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
0|gentle-s |   code: 'MODULE_NOT_FOUND',
0|gentle-s |   path: '/root/my-app/package.json',
0|gentle-s |   requestPath: '/root/my-app'
0|gentle-s | }

you can see the path is "/root/my-app/index.js"m, but in my package.json scripts , it is

 "scripts": {
    "build": "rm -rf dist && tsc",
    "server": "nodemon dist/index.js",
    "start": "NODE_ENV=production node dist/index.js",
    "watch": "tsc -w",
    "test": "echo \"Error: no test specified\" && exit 1",
    "typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js -d ./dist/data-source.js"
  },

I tried to edit my package.json , but it seem not effect to pm2 , am i missing something?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source