I'm storing an object (which has several methods) into process.env
as below:
var obj = createObject(); // returns new object process.env.OBJ = obj;
When I access this object from other places like below, I don't see any of the methods.
var obj = process.env.OBJ;
Showing [Object Object]
.
Why is that?
The process. env global variable is injected by the Node at runtime for your application to use and it represents the state of the system environment your application is in when it starts. For example, if the system has a PATH variable set, this will be made accessible to you through process. env.
Simple answer is YES, . env is used to store keys and secrets. It is not pushed to your repo i.e. github or bitbucket or anywhere you store your code. In that way it is not exposed.
Short answer is: NO
No, you can't store objects in process.env
because it stores environment variables like PATH, SHELL, TMPDIR
and others, which are represented by String values. If you run command console.log(process.env);
you can see all env variables of your system, in particular you can set your own env variables (e.g. process.env.home = 'home'
) which will be available during the process you run your nodejs application.
Solution exists!
Stringify JSON object and save as env variable. Then parse and use it when you need your object
process.env is to store your environmental variables not really to store your objects. You can store your variables like that:
process.env['CONSUMER_KEY'] = "" process.env['CONSUMER_SECRET'] = "" process.env['ACCESS_TOKEN_KEY'] = "" process.env['ACCESS_TOKEN_SECRET'] = ""
Here is a link to it https://nodejs.org/api/process.html#process_process_env
If you want to store your methods you should create a global object and assign your methods to that one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With