1

I've tried declaring them in package.json like this

"scripts": {
  "start": "MY_ENV=hello MY_ENV2=foo meteor"
}

But I got undefined in my code (using envs on a server side) accessing them via process.env

I know about other ways of declaring that kind of sensitive data (like in settings.json), but I want to know how to do in another way (I'm using macos)

2 Answers 2

3

Command line settings are available to your app, but only on the server. In client code, process.env doesn't have them. You can write a Meteor method, so that the client can ask for environment settings. There are potential security risks with this approach because you are allowing access to server settings on the client and potentially revealing them to the user.

You could put settings in a database collection, and your code will read them from there. That's probably a better way, as each deployment can easily have different settings.

If you want to use process.env, you can either do it on the command line,

MY_ENV=xxx meteor --port 3010

Or you can put them in a shell script like this

#!/bin/bash export MY_ENV=xxx export MY_ENV2=yyy meteor --port 3010

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for providing me with alternatives, but I'd still like to know about loading env variables from a file. After some small research, I've found out that it's possible to put all environment variables into a .sh file, but I still don't know how to tell meteor to use it
Do you want access to them on the server or the client? How are you deploying your app, because that will make a difference. If you use mup, you simply create a settings.json file, which makes it very easy
I've mentioned in my question that I'm using them on a server. For now, production in not a big concern as I'm only testing things out. Anyway, I guess, now I'm closer to the answer. If I type in my console export X=foo, then I can access it via process.env.X. I've tried to put all of my envs into .sh and executing it with sh path/to/.sh command` but got nothing
Thanks A LOT! That's what I needed
0

@Mikkel 's answer helped me a lot, but I still got problems with Meteor's default environment variables. So, the solution that worked for me (on macOs) was:

 #!/bin/bash
MONGO_URL="my-secret" 
SOME_API_KEY="my-another-secret" 
FOO=40 
BAR=25 
meteor --settings settings-local.json  --port 300

Executing this .sh file initialized all environment variables

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.