17

I like my node.js so much, that I want to use it is my bash start up script ~/.bashrc, but I do not know how to export variable.

Currently I have to use this approach:

export PS1=`node ~/PS1.js`
export PS2=`node ~/PS2.js`
export PATH=`node ~/PATH.js`

instead I want .bashrc look have

#!/usr/local/bin/node
//do something, define functions
export_var('PS1', PS1())
export_var('PS2', PS2())
export_var('PATH', generatePATH())

process.env.PATH = something does not export, only sets for the currently executing process, which is node itself.

5
  • possible duplicate of How to change value of process.env.PORT in node.js? Commented Oct 20, 2014 at 11:08
  • try to use "#." as the 1st line and keep the first block of code Commented Oct 20, 2014 at 11:10
  • 2
    @rafaelcastrocouto, you do not understand the question and are trying to close it instead? I need to change the variable from inside node.js and let the parent process keep it. The question you are referring to is about changing PORT variable before the node.js starts. Commented Oct 20, 2014 at 11:18
  • You need get the text? You need open process manually inside your main process? Use ' instead ` on .bashrc exports and get the commands as string. Commented Oct 20, 2014 at 11:22
  • @GabrielBiga, I do not need to get the text of the variable, I need to set it persistently, this is called "export variable" in Linux. Commented Oct 20, 2014 at 11:27

4 Answers 4

13

Node.js will run in an separate process which gets a copy of the environment. You cannot change the environment of you parent process (the one executing .bashrc).

But the following question has an answer for you: Can a shell script set environment variables of the calling shell?

You can write a new script file from within node.js and call it via source.

Sign up to request clarification or add additional context in comments.

2 Comments

Could you add a code example for this as I don't understand how I can source a node.js script: "You can write a new script file from within node.js and call it via source."
How it cal work? Script should be executed in the parent shell, but this is not possible.
12

One possible way is to use JS to print out the export statements, then in shell to use eval to evaluate it in the current shell.

e.g. test.js

#!/usr/bin/env node
console.log('export A=40; export B=10');

In the shell:

eval `./test.js`
echo $A

1 Comment

This was simple and clean magic! For those using yarn, use yarn --silent <script> when running your command.
0

Create a file named `.env` with your exports.

export MY_VAR="value1"
export MY_OTHER_VAR="value2"

Then launch your app.js

node app.js --env-file=/path/to/.env

Inside your app.js, you just need to look for process.env.MY_VAR and process.env.MY_OTHER_VAR

Comments

-1

In my case it works:

const exportVar = (name, value) => {
    process.env[name] = value;
};

exportVar("NODE_ENV", "test");

But also in some cases it can be useful to play with exec (or spawn):

const { execSync } = require('child_process');

execSync("NODE_ENV=test && echo $NODE_ENV", {
    stdio: 'inherit',
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.