简体   繁体   中英

Use of pm2 with npm run

I havea react frontendapp for which I defined a number of build and run tasks in package.json as shown in the following snippet:

 "scripts": {
    "start": "env-cmd -f .env.dev react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "build:dev": "env-cmd -f .env.dev npm run build",
    "build:test": "env-cmd -f .env.test npm run build",
    "build:prep": "env-cmd -f .env.prep npm run build",
    "build:prod": "env-cmd -f .env.prod npm run build",
    "start-dev-server": "env-cmd -f .env.dev node server/server.js",
    "start-test-server": "env-cmd -f .env.test node server/server.js",
    "start-prep-server": "env-cmd -f .env.prep node server/server.js",
    "start-server": "env-cmd -f .env.prod node server/server.js"
  },

My goal is to run the app by using the pm2 tool based on a run configuration which should execute "npm run start-server". This execution should internaly run the last line in the above snippet which is "env-cmd -f.env.prod node server/server.js"

I wrote a pm2 config file, namely my_config.json, for to realizing above sketched scenario as follows:

my_config.json:

{
  "apps": [
    {
      "name": "ReactFrontEndApp",
      "script": "npm",
      "args": "run start-server"
    }
  ]
}

Finally, I issue the following command in the command console:

pm2 start my_config.json

When above pm2 command is issued my application is listed under pm2 list command output. Nevertheless, the pm2 tool also starts popping up command-consoles one after another without an end (the previos one fades away and then the next one appears). I tested my application at https://localhost:3000 in the browser meanwhile but the browser does not bring anything my app at all (ie not found). As a consequence, I have to stop my application by using pm2 stop command.

QUESTION: Is there something I might be missing in the my_config.json file, which might be causing this successive command console opennings? What could be wrong?

I guess that your server fail to load somehow, maybe pm2 change relevant environment variables.

what is the output of pm2 logs 0 ? what happened when you run npm run start-server manually? what happened when run pm2 start npm -- run start-server manually?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM