Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up process.env variables using EXPORT while running node with sudo

Tags:

node.js

sudo

I'm using node.js on EC2

I type

EXPORT PORT=80

in terminal, and i see that it correctly saves it when i type EXPORT

But when I run my node.js app with the following:

...
console.log(process.env);
...

PORT is not listed in the object when I'm running it with sudo:

sudo node app.js

How do I set PORT so that I can access it from the process.env object while running node with sudo?

like image 368
gotta have my pops Avatar asked Jan 11 '13 12:01

gotta have my pops


People also ask

How do I export an environment variable?

To export a environment variable you run the export command while setting the variable. We can view a complete list of exported environment variables by running the export command without any arguments. To view all exported variables in the current shell you use the -p flag with export.

How do I run an env file in Node?

env files in Node. js. To allow developers to quickly swap between many customized environment files during development, we can configure dotenv to load environment variables from a custom file via the DOTENV_CONFIG_PATH environment variable. Then, set the DOTENV_CONFIG_PATH environment variable to .

What is process env NODE_ENV === development?

It's a system environment variable that Node exposes to your application, and apparently the Express web server library popularized using its value to determine whether to do optimizations or not.


1 Answers

To set process.env variable use the following code:

sudo PORT=80 node server.js

Of course, you can set multiple process.env variables:

sudo PORT=80 HOST=localhost node server.js

Normally, EXPORT should work too. But sudo creates its own environments and then starts your program as root. So, you shall either add PORT to sudo's environment or force it to preserve your own environment.

To change sudo's environment you shall modify /root/.profile.

To force it to preserve your own environment use -E key:

sudo -E node app.js
like image 62
Leonid Beschastny Avatar answered Oct 19 '22 12:10

Leonid Beschastny