Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set an environmental variable in node.js and then use it in terminal?

well I'd like to add an environmental varible during the execution of a js file using node.js.

Something like: process.env['VARIABLE'] = 'value';

I'm using the terminal to run the js file using a module, I can set the variable and then use it in during the js file execution, but I'd like to set the variable using "process.env", and then when the execution ends, I'd like to use it in the terminal, or in another js process as well.

I know that it's possible using the child_process.exec, and use SET (Windows) or EXPORT (Mac & Linux), but just asking how it can be possible it can be possible, or using which design or process to add it, just using "process.env".

Thanks in advance, folks.

like image 452
MartinCGG Avatar asked Mar 27 '14 07:03

MartinCGG


People also ask

How do I set an environment variable in terminal?

To set an environment variable, use the command " export varname=value ", which sets the variable and exports it to the global environment (available to other processes). Enclosed the value with double quotes if it contains spaces. To set a local variable, use the command " varname =value " (or " set varname =value ").

How do I set an environment variable for Node modules?

You can load the package dotenv in the js file. You need to load the dotenv package in your project and access the environment variable in your need variable. Same as the above example here. var port = normalizePort(process.


1 Answers

The unix permissions model will not allow a child process (your node.js app) to change the environment of its parent process (the shell running inside your terminal). Same applies to current working directory, effective uid, effective gid, and several other per-process parameters. AFAIK there's no direct way to do what you are asking. You could do things like print the command to set it to stdout so the user can easily copy/paste that shell command into their terminal, but your best bet is to explain the broader problem you are trying to solve in a separate question and let folks tell you viable ways to get that done as opposed to trying to change the parent process's environment.

One possible workaround would be something is simple as running your node program from the terminal like this:

export SOME_ENV_VAR="$(node app.js)"

and have app.js just print the desired value via process.stdout.write.

Second hack would be a wrapper shell script along these lines:

app.sh

#!/bin/bash
echo app.sh running with SOME_ENV_VAR=${SOME_ENV_VAR}
echo "app.sh running app.js"
export SOME_ENV_VAR="$(node app.js)"
exec /bin/bash

app.js

console.log("Some Value at " + Date());

Running this in an interactive shell in your terminal

echo $SOME_ENV_VAR

exec ./app.sh
app.sh running with SOME_ENV_VAR=
app.sh running app.js

echo $SOME_ENV_VAR 
Some Value at Thu Mar 27 2014 08:13:01 GMT-0600 (MDT)

Maybe these will give you some ideas to work with.

like image 100
Peter Lyons Avatar answered Nov 15 '22 23:11

Peter Lyons