I am fairly new to node.js, and have a program that I have to set an environment variable in order to run (using the noble library, my bash command is: sudo NOBLE_HCI_DEVICE_ID=x node program.js
, to tell my code which Bluetooth adapter - HCI device - to use).
我对node.js相当新,并且有一个程序我必须设置一个环境变量才能运行(使用高级库,我的bash命令是:sudo NOBLE_HCI_DEVICE_ID = x node program.js,告诉我的代码哪个蓝牙适配器 - HCI设备 - 使用)。
The reason behind this is that I have a number of modules, each needing their own Bluetooth adapter, and I wish to specify in my code which adapter each module should use.
这背后的原因是我有许多模块,每个模块都需要自己的蓝牙适配器,我希望在我的代码中指定每个模块应该使用哪个适配器。
I've found lots of articles telling me how to consume environment variables in my code and set them via the command line (process.env.VARIABLE_NAME), but nothing telling me how to set them from within node.js.
我发现很多文章告诉我如何在我的代码中使用环境变量并通过命令行(process.env.VARIABLE_NAME)设置它们,但没有告诉我如何在node.js中设置它们。
Is it possible to set the environment variables in my node.js code?
是否可以在我的node.js代码中设置环境变量?
Thanks
谢谢
3 个解决方案
#1
24
You can not only consume environment variables in node with process.env
but also set them. This will set the variable within your current node process and any child processes it calls, but not the calling shell itself.
您不仅可以使用process.env在节点中使用环境变量,还可以设置它们。这将在当前节点进程中设置变量,并在其调用的任何子进程中设置该变量,而不是调用shell本身。
// consume
var alreadySetEnvVarForDevice = process.env.NOBLE_HCI_DEVICE_ID
// set
process.env['NOBLE_HCI_DEVICE_ID'] = 1
#2
-3
If you are using express, you can set the variables as follow:
如果您使用express,可以将变量设置如下:
var express = require('express');
var app = express();
// set the environment mode, default is process.env.NODE_ENV
app.set('env','development');
app.get('env');
// => 'development'
#3
-3
run command like following in command prompt
在命令提示符下运行命令如下
export FOREVER_ROOT=/var/log/
Here export set a environment variable
这里导出设置一个环境变量
OR
要么
Execute "/etc/environment" in every shell where you want the variables to be updated:
在要更新变量的每个shell中执行“/ etc / environment”:
$ /etc/environment
$ / etc / environment
#1
24
You can not only consume environment variables in node with process.env
but also set them. This will set the variable within your current node process and any child processes it calls, but not the calling shell itself.
您不仅可以使用process.env在节点中使用环境变量,还可以设置它们。这将在当前节点进程中设置变量,并在其调用的任何子进程中设置该变量,而不是调用shell本身。
// consume
var alreadySetEnvVarForDevice = process.env.NOBLE_HCI_DEVICE_ID
// set
process.env['NOBLE_HCI_DEVICE_ID'] = 1
#2
-3
If you are using express, you can set the variables as follow:
如果您使用express,可以将变量设置如下:
var express = require('express');
var app = express();
// set the environment mode, default is process.env.NODE_ENV
app.set('env','development');
app.get('env');
// => 'development'
#3
-3
run command like following in command prompt
在命令提示符下运行命令如下
export FOREVER_ROOT=/var/log/
Here export set a environment variable
这里导出设置一个环境变量
OR
要么
Execute "/etc/environment" in every shell where you want the variables to be updated:
在要更新变量的每个shell中执行“/ etc / environment”:
$ /etc/environment
$ / etc / environment