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.
Related
i have a express server like this
app.use(['/home', '/'], express.static('dist', { maxAge: cacheTime}));
dist is my folder where i build my react app, everthing works fine but now i need to know the enviroment where the app is so i thought i could get that with
process.env.NODE_ENV
however i don't know if it's posible to send that value to my react app i've been doing a lot of research on google but i can't find anything yet
any suggestions?
thanks in advance
Are you running the server locally or on a hosted service?
If you are running it locally, you have to set the node environment yourself.
Install the dotenv npm package and setup your .env file
process.env.NODE_ENV is a reference to the NODE_ENV value in your .env file
If you are running it on the hosted service, process.env.NODE_ENV should get the environment of the service, which is likely to be "production". Or you should check out the documentation on their NodeJS support.
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
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.
When I'm in dev mode I don't want my server to send email and some other stuff so I have this in server.js file:
process.env.NODE_ENV = 'development';
When I move it to production i change the value to 'production'.
Problem is of course that it is easy to forget on deployments. Is it possible somehow to detect when server is in production?
If it's not, is it possible to write a batch script that replaces a string in a file?
You shouldn't manually change values of process.env object, because this object is reflecting your execution environment.
In your code you should do the following:
const environment = process.env.NODE_ENV || 'development';
And then you launch your app in production like this:
$ NODE_ENV=production node app.js
If NODE_ENV environment variable is not set, then it will be set to development by default.
You could also use dotenv module in development to specify all environment variables in a file.
Furthermore, I've implemented a reusable module, which allows to automatically detect environment by analyzing both CLI arguments and NODE_ENV variable. This could be useful on your development machine, because you can easily change environment by passing a CLI argument to you Node.js program like this: $ node app.js --prod. It's also nice to use with Gulp: $ gulp build --prod.
Please see more details and use cases on the detect-environment's page.
The suggested way to tackle these kind of problems is by using the process global object provided by NodeJS. For example:
var env = process.env.NODE_ENV || "development";
By the above line. We can easily switch between development / production environment. If we haven't configured any environment variable it works with development environment.
process.env is an object that contains the user environment.
For example if we are running our application in a bash shell and have configured NODE_ENV to production. Then i can access that in node application as process.env.NODE_ENV.
There are multiple ways to define this NODE_ENV variable for nodejs applications:
## Setting environment variable for bash shell
export NODE_ENV=production
## Defined while starting the application
NODE_ENV=production node app.js
So i don't think you would require any batch file for this. When handling multiple configuration variables, follow this.
This is normally handled through configuration files (e.g. config frameworks like node-convict) and through environmental variables. In the start command in production, you might do something like:
NODE_ENV=production npm start
then the process.env.NODE_ENV would be properly set by any module in your app that cares.
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