5

I'm writing a Node.js express app and want to use environment variables for setting the port on which the server should run.

However, I can't seem to get process.env.PORT to read my PORT environment variable.

I've defined the PORT environment variable using export like so: export PORT=1234 I've also added this line to the ~/.bash_profile file, but process.env.PORT remains undefined.

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

I'm running Node V0.12.7 and OSX El Capitan 10.11.1 and really can't find any extra clues on what might be causing this.

Thanks!

EDIT: Here's the code executed before trying to assign process.env.port to the port variable

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);
Niekert
  • 917
  • 1
  • 8
  • 20
  • Are you running your command inline (`node -e 'process.env.PORT'`)? If so it is normal that you can't access the `process` object. – 0x9BD0 Nov 16 '15 at 14:47
  • Not entirely sure what you mean by running my command inline. I'm running my program running `node server` (`server.js` is the main file). Other environment variables such as `HOME` (`process.env.HOME`) are defined and accessible – Niekert Nov 16 '15 at 15:21
  • Can you show the code of your `server.js`? And by the way maybe you should update your node version because we are now at version 4.2. – 0x9BD0 Nov 16 '15 at 15:30
  • Please see my edit for the code. I also tried moving the assignment of the `port` variable to the first line but the same problem occurs. I did not update to node 4.2 because there are some issues with a third party dependency I'm using, but I'll upgrade now to test if that's the issue. – Niekert Nov 16 '15 at 15:39
  • did you ever figure this out? I'm having the same issue – ekkis Apr 20 '16 at 00:36
  • I didn't find out what was causing the issue, but did manage to get it fixed. First I had node installed through the installer of the Node.js website. I removed that installation and installed Node the proper way with [homebrew and nvm](http://stackoverflow.com/a/28025834/1890026). The issue was gone after that. Maybe not the answer you're looking for (especially if you're not on OSX), but it may be worth a try! :) – Niekert Apr 20 '16 at 08:51
  • Had this problem, too. In my case I only had `PORT=1234` in my `.zshrc` file, which could be retrieved in standard Terminal but not in Node. Solved it by changing it to `export PORT=1234` - in a way your question already had the answer for me in it :) – Felix Jassler Jan 21 '21 at 14:54

2 Answers2

4

There's a great npm package called dotenv, that will auto-load any environment variables from a file called .env in the same directory as your nodeJS application. This could give you a differentPORT variable project to project, instead of globally defining one PORT across all terminals.

As far as everything else, it sounds like PORT should be there (you even said it was properly echo-ing from command line). Did you run node out of the same terminal as you used echo? The only other issues I can think of are if you didn't restart the terminal you're running your server out of after you modified your ~/.bash_profile, or maybe a simple typo somewhere.

I would have posted this as a comment, but I don't have the score for it.

Austin Ezell
  • 753
  • 4
  • 12
  • Thanks for the `dotenv` suggestion. I will use that package for as long as my environment variables are not read correctly! Also, I did in run node from the same terminal as I did `echo $PORT` in, also restarted the terminal. I'm not marking your answer as the accepted answer in case someone has an answer to what might be causing my environment variables to not be read by Node. :) – Niekert Nov 18 '15 at 09:20
  • Thanks for the suggestion to restart the terminal. I set up autoenv and was confused why `$echo` was working but not `console.log(.envStuff)` in my `app.js`. Fixed my issue. – m00saca Feb 19 '17 at 21:47
  • thanks for suggesting running node out of the same terminal – SunDontShine Nov 13 '18 at 19:58
0

Seems like there is a function or another object named process, it could cover the NodeJS process object.

troy
  • 2,145
  • 2
  • 23
  • 31