PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, November 5, 2022

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

 November 05, 2022     bash, environment-variables, javascript, node.js     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing