简体   繁体   English

如何在javascript中设置环境变量

[英]How to set environment variables in javascript

I'm trying to set some environment variables which can be picked up by my bash script.我正在尝试设置一些可以由我的 bash 脚本获取的环境变量。

order of execution:执行顺序:

version.js版本.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=

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. export在 bash 进程本身中起作用的唯一原因是因为您通过“采购”它们在 bash 进程中运行这些脚本,因此它正在修改自己的环境变量。

Example例子

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

prints nothing because test.js was run in its own process什么都不打印,因为test.js在它自己的进程中运行

$  source test.sh
echo $FOOBAR

prints 'Testing' because in this case test.sh was read and processed by the current bash process itself.打印 'Testing' 因为在这种情况下 test.sh 是由当前 bash 进程本身读取和处理的。

The best you could really do is export a shell script that the shell then executes您真正能做的最好的事情是导出一个 shell 脚本,然后 shell 执行该脚本

// 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.但当然,该输出特定于您正在运行的 shell,这意味着如果您切换 shell,那么您需要输出的内容会发生变化。 It's also not a normal way to do this.这也不是执行此操作的正常方法。

A more common 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 .您可以使用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,您可以在其中维护文件中的所有 env 变量并适当地使用它们,这有助于更好地进行版本控制,因为它将配置存储在与代码分开的环境中。

Dotenv is a zero-dependency module that loads environment variables from a .env file into process.env. Dotenv是一个零依赖模块,它将环境变量从 .env 文件加载到 process.env 中。

Usage 1. Create a .env file用法1. 创建一个 .env 文件

DB_HOST=localhost
DB_USER=root
DB_PASS=s1mpl3

2.process.env now has the keys and values you defined in your .env file. 2.process.env 现在具有您在 .env 文件中定义的键和值。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM