Process.env undefined variables even after export

I am writing a Node.js express application and want to use environment variables to set the port on which the server should run.

However, I cannot get process.env.PORTto read my environment variable PORT.

I defined the PORT environment variable using the exportfollowing: export PORT=1234I also added this line to the file ~/.bash_profile, but process.env.PORTremains undefined.

When I run echo $PORTin the terminal, it displays the value ( 1234) correctly .

I am running Node V0.12.7 and OSX El Capitan 10.11.1 and really can't find any further hints about what might be causing this.

Thanks!

EDIT: Here is the code executed before trying to assign to a process.env.PORTvariablePORT

var app = require('../app');
var proxy = require("../proxy");
var http = require('http');
var nconf = require('nconf');
nconf.file(__dirname + "/appConf.json");
var appSettings = require('../appSettings');

var port = normalizePort(process.env.PORT || 8080);
+4
source share
1 answer

There is a large npm package called dotenvthat will automatically load any environment variables from a file with a name .envin the same directory as the nodeJS application. This may give you a different project variable PORTfor the project, rather than globally defining one PORTfor all terminals.

, , PORT ( , echo - ). node , echo? , , - , , , ~/.bash_profile , , -.

, .

+3

Source: https://habr.com/ru/post/1616070/


All Articles