For some reason I can't set the environment variables on my node js app. I am currently trying to use dotenv package to set my environment variables and I put a log statement in the config() method to print out the processes.env variable and it looks like it adds the variables correctly. However, once I'm back in my index.js when my app runs, logging the process.env only shows the following environment variables:
NODE_ENV : "development"
PUBLIC_URL : ""
I've tried just about everything. Why can't I set the environment variables for my node app?
Related
Environment variables in NodeJS depenency
I have a NodeJS application which has a dependency of mine
my-base-module: git+https://myuser:mytoken#gitlab.com/organization/my-base-module.git#v1.0.0
I am also using the dependency dotenv, which in development I use the file .env and in other environments I pass the variables through docker environment variables
The problem I have, and I do not know why and how to solve it is that in my-base-module I do not share the same environment variables (and I need it to). It is like the variables loose the reference
require('dotenv').config();
const env = process.env.NODE_ENV;
for example in that bit of code the NODE_ENV variable inside my-base-module is undefined. However in the container is defined and with the right value
Update 1
I am requiring dotenv in both my-application and my-base-module. If I enter to the container and I do $ echo $NODE_ENV. I get production.
If my application process.env.NODE_ENV also holds production. But in the dependency process.env.NODE_ENV is undefined
I will try to do some github repositories to reproduce it
I was wrong, this works I made this small POC with these public repositories
https://github.com/agusgambina/try-base-module..https://github.com/agusgambina/try-application-dotenv
https://github.com/agusgambina/try-application-dotenv
I'm learning how to deploy a simple backend node.js project to production, in my case heroku. I came across this note regarding environment variables in a .env file:
The environment variables defined in dotenv will only be used when the backend is not in production mode, i.e. Heroku.
We defined the environment variables for development in file .env, but the environment variable that defines the database URL in production should be set to Heroku with the heroku config:set command:
heroku config:set MONGODB_URI=mongodb+srv:...
How can my .env file use the heroku config:set MONGODB_URI=mongodb+srv... environment variable, if using heroku would mean that my backend is in production mode? The first sentence states that environment variables are only used for development mode.
What am I understanding wrong? Are environment variables used in both development mode and production mode, and the wording of the note I read was wrong?
I think it's saying that environment variables in .env will be used when you're in development (not Heroku) and that if you do want to use your environment variables in Heroku that you need to do it through heroku config:set ....
What am I understanding wrong? Are environment variables used in both development mode and production mode, and the wording of the note I read was wrong?
Environment variables are used in all scenarios, but only in local development mode they are read from a file.
dotenv is a package that allows reading environment variables from a file.
In production mode, what Heroku is essentially doing is this, for example:
PORT=9999 node index.js
Try it, you can access this variable inside Node by using:
console.log(process.env.PORT);
// 9999
Environment variables are just global variables available to the entire process.
How can we set environment variables in NodeJs
I have tried this in my terminal
NODE_ENV=prod
node index.js
and then inside my code when I try to log it (config.js file which is imported in index.js and where i configure everything)
console.log(process.env.NODE_ENV)
it gives me undefined
Use
export NODE_ENV=prod
also you should create a .env file and keep all your environments in the same.
In your config file set the default values
NODE_ENV = process.env.NODE_ENV || 'prod';
Even though the dashboard for the Lambda function shows me NODE_ENV listed as an environment variable, process.env.NODE_ENV is undefined when I run my Node AWS Lambda function.
This code...
console.log('node process env NODE_ENV:');
console.log(process.env.NODE_ENV);
logs this:
node process env NODE_ENV:
undefined
I was wondering if it was a role issue, so I gave its role all permissions on AWS Lambda, but it didn't help.
For what it's worth, I'm using Claudia bot builder, running the npm script
"deploy:production": "claudia update --version production --set-env NODE_ENV=production",
But like I say, the NODE_ENV environment shows in the list of environment variables in the dashboard for the function.
Any ideas?
Turns out the problem was that Webpack compressed away process.env.
I got around the issue by using babel-plugin-transform-inline-environment-variables, which inlines the environment variables at build-time.
If you got here from a google search:
This could also happen when you define a function named "process" in your code. The function will override the internal node.js "process" variable.
I found out as long as you put target: node in your webpack configuration, you will get access to the process.env variables.
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.