skip to Main Content

I make my scripts on windows then put them on my ubuntu server. This is my first time trying to pass args into the script on start up.
It works on windows I run

node file.js --variable=value

Then in my code I grab it with

const passedIn = require(argv.variable);

I cant figure out how to make this work with pm2.

I have been trying

pm2 start file.js --name myScript --node-args="value"

I didnt expect this to work. What do I need to call to access my passed in arg correctly from the code instead of argv.variable? and can I still do something like

--node-args="variable=value"

2

Answers


  1. Chosen as BEST ANSWER

    I was able to do it like this

    pm2 start script.js --name myScript -- -v=value

    and access it in code with

    var argv = require('minimist')(process.argv.slice(2));

    console.log(argv.v);

    it only seemed to allow 1 letter variables.


  2. Try this:

    pm2 start file.js --name myScript --node-args="--variable=value -port 12345"
    

    Alternatively, add --, followed by your script args:

    pm2 start file.js --name myScript -- --variable=value -port 12345
    

    Details: https://futurestud.io/tutorials/pm2-how-to-start-your-app-with-node-js-v8-arguments

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search