14

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.

exebook
  • 32,014
  • 33
  • 141
  • 226
  • possible duplicate of [How to change value of process.env.PORT in node.js?](http://stackoverflow.com/questions/13333221/how-to-change-value-of-process-env-port-in-node-js) – rafaelcastrocouto Oct 20 '14 at 11:08
  • try to use "#." as the 1st line and keep the first block of code – rafaelcastrocouto Oct 20 '14 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. – exebook Oct 20 '14 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. – GabrielBiga Oct 20 '14 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. – exebook Oct 20 '14 at 11:27

3 Answers3

11

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.

Community
  • 1
  • 1
delixfe
  • 2,471
  • 1
  • 21
  • 35
  • 2
    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." – Molten Ice Mar 07 '21 at 14:44
  • How it cal work? Script should be executed in the parent shell, but this is not possible. – zdm Jul 19 '23 at 13:16
11

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
Jianwu Chen
  • 5,336
  • 3
  • 30
  • 35
-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',
});
zemil
  • 3,235
  • 2
  • 24
  • 33