Saturday, November 5, 2022

[FIXED] How to export env variable in node.js

Issue

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.


Solution

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.



Answered By - delixfe
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.