bash - How to set environment variables in javascript - Stack Overflow

I'm trying to set some environment variables which can be picked up by my bash script.order of exe

I'm trying to set some environment variables which can be picked up by my bash script.

order of execution:

version.js

module.exports.version = function () {
   process.env['APP_VERSION'] = `1.0.0`;
}

script.sh

run x 
run y
node -e "require('./version.js').version()"
echo "APP_VERSION=$APP_VERSION"

but echo output is

APP_VERSION=

I'm trying to set some environment variables which can be picked up by my bash script.

order of execution:

version.js

module.exports.version = function () {
   process.env['APP_VERSION'] = `1.0.0`;
}

script.sh

run x 
run y
node -e "require('./version.js').version()"
echo "APP_VERSION=$APP_VERSION"

but echo output is

APP_VERSION=
Share Improve this question asked Jan 1, 2020 at 5:11 ir2pidir2pid 6,17615 gold badges68 silver badges110 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

This is not possible. It's not an issue with node, it's just not possible period even any other language. If a child process could modify the environment variables of its parent that would be a huge security issue.

The only reason export works in the bash process itself is becuase you run those scripts in the bash process by "sourcing" them so it's modifying its own environment variables.

Example

#!/bin/sh
# test.sh
export FOOBAR=Testing
$ ./test.sh
echo $FOOBAR

prints nothing because test.js was run in its own process

$  source test.sh
echo $FOOBAR

prints 'Testing' because in this case test.sh was read and processed by the current bash process itself.

The best you could really do is export a shell script that the shell then executes

// example.js
const key = 'FOOBAR';
const value = `hello world: ${(new Date()).toString()}`;
console.log(`export "${key}"="${value}"`)
node example.js | source /dev/stdin 
echo $FOOBAR

But of course that output is specific to the shell you're running in meaning if you switch shells then what you need to output changes. It's also not a normal way to do this.

A more mon way might be to output just the value from node

run x 
run y
$APP_VERSION=`node -e "console.log(require('./version.js').version())"`
echo "APP_VERSION=$APP_VERSION"

You can use ShellJS.

var shell = require('shelljs');
shell.echo('APP_VERSION="0.0.1"');

Make use of Dotenv, where you can maintain all the env variables in a file and use the same appropriately, helps in better versioning, since, it Stores configurations in the environment separate from code.

Dotenv is a zero-dependency module that loads environment variables from a .env file into process.env.

Usage 1. Create a .env file

DB_HOST=localhost
DB_USER=root
DB_PASS=s1mpl3

2.process.env now has the keys and values you defined in your .env file.

const db = require('db')
db.connect({
  host: process.env.DB_HOST,
  username: process.env.DB_USER,
  password: process.env.DB_PASS
})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745205629a4616580.html

相关推荐

  • bash - How to set environment variables in javascript - Stack Overflow

    I'm trying to set some environment variables which can be picked up by my bash script.order of exe

    11小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信