process.env variables are undefined even after export - node.js

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);

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.

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

Related

Using the environment variable crashes the nodejs app

The app runs on a shared server at namecheap. When I hardcode the env variable as a regular variable inside server.js like let MONGO_URL=blablabla the app works OK.
When i use process.env.MONGO_URL instead, it crashes. Any ideas why it is happening?
note: I have my env variable installed on node console of cpanel.
note2: The problem is not MONGO_URL, using process.env.PORT crashes the app too.
is there process.env.MONGO_URL in list of variable that you installed on node console of cpanel?
if it is not, put it on that list and try again
Solved.
Added to the server.js
if (process.env.NODE_ENV != 'production') {
require('dotenv').config();
}
And switched the app mode to "production". Now process.env works.

not able to set process.env.PORT variable in windows for node

const express = require('express');
const api = express();
api.get('/', (req, res) => res.send('Hello World'));
var port = process.env.PORT || 3000;
api.listen(port, () => console.log(`Listening to port
${port}`));
I am using this command to
set PORT=5000
After setting it stills show Listening to port 3000
You must run your file with the same CLI program (and instance) you used to set the variable value.
In Command Prompt: set PORT=5000
In Power Shell: $env:PORT=5000
In Bash (Windows): export PORT=5000
After that, run your program node app.js and it works.
If you're using the integrated terminal of VSCode to run your file, but using another CLI program (or instance) to set the value of PORT, is not going to work unless you change that value permanently by using setx PORT 5000 either with the Command Prompt or Power Shell (I don't know how to do it from Bash for Windows). In that case, you would need to restart VSCode for running your file with the last value of PORT.
It should be like this
For Command Prompt: set PORT=5000
For Power Shell: $env:PORT=5000
For Bash (Windows): export PORT=5000
Not like this
For Command Prompt: set PORT = 5000
For Power Shell: $env:PORT = 5000
For Bash (Windows): export PORT = 5000
i.e. no space before and after = sign.
use set PORT=5000 in command prompt rather than vscode terminal. It surely will work.
If you are using powershell or vscode terminal
$env:PORT=5000
if you are using windows command prompt
set PORT = 5000
if you are using Bash or MAC terminal
export PORT = 5000
for me i am using vscode terminal and $env:PORT=5000 is working fine.
const port = Number(process.env.PORT)
I don't know if we had the same problem, or if you ever figured this out, but I made an account JUST for this because I could not for the life of me get process.env.PORT to work, even though I was following everyone's code to the tee (no one else seems to have to explicitly convert process.env.PORT to a number). Needed to fix this problem in order to deploy my app on Heroku, which assigns the PORT variable. Can happily say it works and not lose sleep tonight :)
Sometimes it gives an error like:
'export' is not recognized as an internal or external command,
operable program or batch file.
use this command prompt (Windows):
set PORT=5000

env is not working in Windows to set port for ExpressJS is there any solution?

I am deploying app on heroku and this app need to listen random port as someone download it. For that I need dynamic port so, env is working well in Linux and OSX but I need to know how to use same in Windows because env is not supporting in Windows! Help me out :)
This is My Code:
const port = process.env.port || 3000; //sets to 3000 but I need to make it dynamic
console.log(process.env.port);// undefined if I remove OR(||) in above statement
It is most likely that you didn't set the env in windows correctly. It should be something like this:
"start_windows": "set port=3333&&node index.js"
or use this package so you can set them the same for all platforms

application.js wont start but debug works

I am new to node.js and I want to get webserver running but I got this problem where trying to debug application by running node ./bin/www works perfectly but trying to launch it by node app.js dosen't do anything.
When i type out node app.js in terminal blank line appears like its loading something and dissapears in few seccods without any error or starting application.
The problem is that app.js is exporting an app object, not starting a server. If you look at the code for bin/www you'll see that it loads the app object and uses it to start a server.
#!/usr/bin/env node
var debug = require('debug')('tmp');
var app = require('../app');
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});
You need to either start the app using bin/www, or modify and move that code to the end of app.js if you don't want the separate start script.
If you want to see the debug messages, you need to add the DEBUG environment variable to your shell session. For development environments, you could do this automatically by placing it in your .bashrc file.
export DEBUG=express:*
Or if you do not want to do that, you can just do this:
DEBUG=express:* node ./bin/www
http://expressjs.com/guide.html#debugging-express

Running console commands through node inspector?

I have connected node inspector to my running node.js program. But I'd like to issue commands through the console - not necessarily on a breakpoint. For example, I have global variables and functions in my server.js program. For example:
var express = require('express');
var app = express();
function test() {
console.log('yo');
}
app.listen(3000);
Now in node-inspector I go into the console and I type 'test()' and it returns "ReferenceError: test is not defined", same things with global variables I type 'app' and it tells me it's not defined. Any idea how to make this thing work? I just want to run my node program and then issue commands to it via a command line interface.
#foreyez,
Your question inspired me to make an npm module that lets you debug (via command line) a running node.js application without setting breakpoints or needing to make variables global:
https://github.com/go-oleg/debug-live
Hope that helps!
The reason why it doesn't work as you expected is that all your variables and functions are local to your module (application) and you can't access them from a global context.
You can save them in the global scope if you want to access them from anywhere (including from the console when not stopped on a breakpoint):
var express = require('express');
var app = express();
function test() {
console.log('yo');
}
app.listen(3000);
global.test = test;

Resources