How to pass arguments to app using pm2?

后端 未结 10 1133
予麋鹿
予麋鹿 2020-12-07 17:29

I am using pm2 to start my app but i am not able to pass argument to it. the command I am using is pm2 start app.js -- dev. Though this works with forever.

10条回答
  •  囚心锁ツ
    2020-12-07 18:03

    I have tested and it works in my windows machine. Below is the complete solution to pass arguments to nodejs app using pm2.

    ** There are also 2 types of argument

    1. node-args - to use before npm start
    2. args - to use in your node program

    There are 2 ways to pass arguments with pm2.

    Option 1: pass by argument with pm2 commands.

    Option 2: by using config file e.g ecosystem.config.js

    Option 1 (Pass arg by commands):

    pm2 start app/myapp1.js --node-args="--max-http-header-size=80000" -- arg1 arg2
    
    //Access the arg as below in your node program.
    console.log(process.argv[2]); // arg1
    console.log(process.argv[3]); // arg2
    

    Option 2 (Using config file): If you are using ecosystem.config.js. you can define with the following configuration:

        {
          name: 'my-app',
          script: 'app\\myapp1.js',
          env: {
            NODE_ENV: 'DEV',
            PORT : 5051
          },
          node_args: '--max-http-header-size=80000',
          args : 'arg1 arg2',
          instances: 1,
          exec_mode: 'fork'
        }
    

    To start as dev mode:

    pm2 start --name myapp  app/myapp1.js -- .\ecosystem.config.js
    

    To start as production mode, just add --env=production

    pm2 start --name myapp  app/myapp1.js -- .\ecosystem.config.js --env=production 
    
    //Access the arg as below in your node program.
    console.log(process.argv[2]); // arg1
    console.log(process.argv[3]); // arg2
    

提交回复
热议问题