Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i store Objects in process.env

Tags:

node.js

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?

like image 222
user1814841 Avatar asked Dec 22 '15 14:12

user1814841


People also ask

What is the use of process env?

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.

How secure is 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.


2 Answers

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

like image 193
Rashad Ibrahimov Avatar answered Sep 21 '22 03:09

Rashad Ibrahimov


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.

like image 35
bdifferent Avatar answered Sep 20 '22 03:09

bdifferent