Setting long environmental variables in nodejs - node.js

On a Node server that will be deployed to heroku, I have to load a long private key from the environmental variables. When I declare my environmental variable in a .env file in the traditional method (key=value, with no linebreaks if value is long), run it locally (through 'heroku local') and console.log the loaded environmental variable value, the console shows it has been split into several lines (but not around the '\n' character).
Loading the string from environmental variable:
Loading the string when it's hardcoded into the server and console logging shows:
I switched to foreman to load environmental variables and used the JSON syntax to get it working. My question is, on the traditional method, why does the console show the string is split up?
Loading the long value as this value is required for Firebase 3 SDK initialisation, as Heroku does not allow you to load the private key file itself.

For local development, the solution is to start the server using Node-Foreman and load the environmental variables from the .env file through the node-foreman utility. Use the JSON format for the environmental variables (not the key=value format). The JSON usage is described in the documentation.

Related

Unable to properly define MongoDB connection string as an Travis CI environmental variable

I'm working on my own project using Node.js and MongoDB as a database (using Mongoose).
When I developed it locally, I defined my environmental variables in a .env file and used 'env-cmd'.
Now I'm trying to create a testing workflow using Travis-CI without using a .env file so I defined my environmental variables on the repository setting but I keep getting an error that the connection string is invalid when trying to build:
It is worth mentioning that:
I escaped every special character when defining the env variable of the connection string.
I tried passing both process.env.MONGODB_URL with and without the ES6 backticks format to the mongoose.connect() method.
The connection string is valid (I tried running my test suites locally with the .env file and it worked)
The connection string is to the MongoDB Atlas database, not a local one.
I'm running the tests from as a docker container.
I can't figure out what is the problem, and if any additional information is needed to tackle this problem I will happily provide.
Thanks in advance!

Can anyone tell me why is node not recognizing my environment variable?

I have a sendgrid API key. I placed it into a dot.env file inside a config folder.
Bud when I try to use it to set api key:
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
Node keeps telling me API key does not start with "SG."
If I try to console.log(process.env.SENDGRID_API_KEY) it gives me undefined.
inside dev.env file:
SENDGRID_API_KEY=myapikey
I also have a PORT variable inside this folder and it is used without problems, so it is not a matter of a path to the file. It is not a typo either.
Are you using something like the "dotenv" package to read de environment variables from the "dev.env" file? You can't read environment variables in NodeJS without something like that if I am not mistaken. When I learned how to import this kind of variables I was teached to name the file just ".env".

Node-Red mongodb3 connect DB using URL from environment variable

I'm running Node-Red embedded in Express application. Also using 'dotenv' to load environment variables.
For storage using MongoDB with 'node-red-contrib-mongodb3'.
Everything works as expected. But, I have different environments and different MongoDB for each environments.
I want to connect to MongoDB from configuration (.env file or environment file).
Something like, in MongoDB config node URL input box golbal.get('env').MONGODB_DEV_URL or msg.MONGODB_URL
Tried looking for an option in the documentation of 'mongodb3' and google, still no luck. Any help or direction will be appreciated.
From the Node-RED docs
Any node property can be set with an environment variable by setting
its value to a string of the form ${ENV_VAR}. When the runtime loads
the flows, it will substitute the value of that environment variable
before passing it to the node.
This only works if it replaces the entire property - it cannot be used
to substitute just part of the value. For example, it is not possible
to use CLIENT-${HOST}.

Why is process.env returning an empty object, while process.env.prop returns the prop value?

So I have the simplest example on a node machine running with a react-redux app with webpack (Though I don't think any of this matters for the issue expect it being on nodejs).
Specific calls get a value pack:
console.log(process.env.NODE_ENV); // output: 'development'
General calls get nothing back:
console.log(process.env); // output: {}
What am I missing here?
Addition info the might be relevant:
I am using dotenv for the test environment.
I am using dotenv-webpack for the development environment.
I am not using neither of those for the production environment deployed to Heroku
The problem persists on all environments.
The issue with process.env variable being empty in browser is because browser doesn't have real access to the process of the node.js. It's run inside the browser though.
Usage of process.env.ANYTHING is usually achieved by plugins like https://webpack.js.org/plugins/define-plugin/ which just simply replace any occurrence of process.env.ANYTINHG with env variable during BUILD time. It really does just simple str.replace(/process.env.ANYTING/value/) this needs to be done during build time as once you output dist bundle.js you don't have access to the ENV variables.
Replacing during build time
Therefore you need to be sure that when you are producing production build e.g with yarn build you are using webpack.DefinePlugin and replacing those process.env calls with current ENV values. They can't be injected in runtime.
Injecting in runtime
When you need to access env variables in runtime it's basically impossible in JavaScript in browser. There are some sort of hacks for example for NGINX which can serialize current env variables to the window.ENV variable and in your app you will not use process.env but window.ENV. So you need to either have ENV variables available while you are building the application or build mechanism which will dynamically output current ENV as json to window and access with react. If you are using docker it can be done with ENTRYPOINT otherwise you need some bash script which will always output current ENV variables as JSON to the index.html of your app

NodeJs Environment variables vs config file

Actually I have a nodejs express app with its config file for params like host, port, JWT token, DB params and more.
The question is if it could have sense to keep those params directly on environment variables (whitout any config file) and acces them without the need of do the "require" for config in all components and modules.
All examples I see uses a config file, probably something about security or memory?
config file is usually for setting the default values for your environment variables,
which is needed when you are writing the test cases and need to use default values or mock values,
and also you will have all the env variables at one place which is better management.
so if you have an environment variable x,
in config file you can keep it as
config.x = process.env.x || 'defaultVale or mockValue'
A config file lets your very quickly set the entire environment of a machine - eg S3 buckets, API urls, access keys, etc. If you separate these into separate process.env.VARIABLE then you would need to set each of these...for which you would likely make a script...and now you have an environment file again!
To access environment variables you can use process.env.VARIABLE in your nodejs code (is always a string), as long as the variable is set before the process is started.
Another possibility is using an .env files in nodejs. I think you have to npm install dotenv in your application. Ideally different instances (dev, prod....) have its own .env file, and you dont have to call require("dotenv") every time if you want to access the environment variable. Call it in the very beginning i.e) in app.js and you can access the environment variable in any of the sub-files.

Resources