5

I would like to pass command line arguments to my Meteor app on start up.

For example --dev, --test or --prod indicating whether or not it is running in dev, test or prod environments. It can then load different resources on start up, etc...

I tried something like this in a /server/server.js

var arguments = process.argv.splice(2);
console.log('cmd args: ' + JSON.stringify(arguments,0,4));

The I ran a test. And quite a few others with just random command line arguments.

meteor --dev

The output in the console is only this.

cmd args: [
    "--keepalive"
]

What is the best way to get command line arguments into a Meteor app?

Or, is this even the correct way to solve the higher level problem? and if not, what is the correct way to solve this problem of distinguishing between running enviro?

Steeve Cannon
  • 3,682
  • 3
  • 36
  • 49

2 Answers2

7

Meteor doesn't forward command line args to your app, if it doesn't know them. You have multiple possibilities:

  1. Rewrite parts of meteor.js to forward unrecognized args. This shouldn't be too hard, but it's not a pretty solution. And if updates occur you are in trouble. :D
  2. You could write a small config file and change the behaviour of your app based on the configuration options in there. Take a look at this question.
  3. The easiest thing to do is using environment variables. You can read env vars in node like this. After that you can start your app the "express.js" way: $ METEOR_ENV=production meteor

I hope I could help you! :)

Community
  • 1
  • 1
jsbeckr
  • 1,183
  • 1
  • 10
  • 24
1

The reason it doesn't work is because the meteor command starts a proxy (which gets the arguments you give) and then it starts the meteor app with --keepalive.

process.argv will have correct values if you build Meteor with meteor build --directory /my/build/path and run it.

Thanish
  • 309
  • 2
  • 5