Reputation: 577
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?
Upvotes: 46
Views: 45677
Reputation: 1182
Try to stringify the object and pass into the process.env and parse the string to get the object this will work. Eg.
var obj = createObject(); // returns new object
process.env.OBJ = JSON.stringify(obj);
Use this in code like let objFromEnv= JSON.parse(process.env.OBJ);
Upvotes: 2
Reputation: 191
Altho you can't store objects in process.env
directly, you can still store in process
itself.
Upvotes: 5
Reputation: 3309
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
Upvotes: 47
Reputation: 703
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.
Upvotes: 21