How to avoid getting wrong data from process.env file? - javascript

I need to get some data from a .env.local file,however instead of getting the data of the file on the current folder i get the one from another project that i had made.
How can I make node recognise the right file to read?
example:
folder-structure:
folder:
file.js
.env.local
js file:
const envData ={
test1: process.env.TEST_1,
test2: process.env.TEST_2,
test3: process.env.TEST_3,
})
.env.local file:
TEST_1=test1-data
TEST_2=test2-data
TEST_3=test3-data
The problem is:I don't get the data from the env file in my folder but from another one,how do I fix this?

I'd recommend using dotenv to manage your environment variables -- it loades environment variables from a .env file into process.env. So you'll have your .env file within your project folder and then whichever folder you need to load your env variables in, you'll require and configure the package at the top of the file like so:
require('dotenv').config();
Which will allow you to call your env variables like process.env.XXX.

Related

How to Set .env with JS

I have this, from https://npm.io/package/node-calendly-sdk.
calendly_client = new Calendly("YOUR-API-TOKEN")
I'm confused on how to set .env varibles in js
Env variables are a set of values, generally to store sensitive data that shouldn't be in the code.
In nodejs, you can add the npm package dotenv with: npm i dotenv, then, you have to create a file named .env in the root directory of your project and define your variable like:
.env file:
API_KEY_TOKEN = "some value"
Then, at the very beginning of your nodejs file, write: require("dotenv").config(); and that's it, you can access your .env variables with process.env
Full example:
require("dotenv").config();
calendly_client = new Calendly(process.env.API_KEY_TOKEN );
For a complete documentation visit dotenv

Nuxt JS env file

I'm using Nuxt JS 2.9.2, and am trying to use a .env file to load a unique encryption key, however, the following doesn't seem to pull the information from the env file, even after installing dotenv
env: {
encryption_key: process.env.ENCRYPTION_KEY || 'secret key 123'
}
The above code is inserted inside of my export default inside of the nuxt config js file, it always seems to load the secret key 123 rather than ENCRYPTION_KEY from the env file
Here are the steps to get this working:
First install dotenv with npm i -D dotenv
Next, make sure you have a .env file that looks something like:
ENCRYPTION_KEY="put your key here"
Finally, add the following to the top of your nuxt.config.js:
require('dotenv').config();
A word of caution
Please be aware that this will actually build your client code with ENCRYPTION_KEY in the source, so anyone could read it. If that isn't what you want, I'd recommend doing all of your encryption on the server.

If I set env vars using dotenv and PM2 ecosystem.config.js, which one will Node use?

I assume PM2 appends env vars the 'native' system way at startup, something like:
MYVAR=hey; node app.js
The difference with the dotenv npm package is it MUST append vars another way, because it works inside the script (it can't do MYVAR=someothervar; node app.js because the program is already started), so it works like this:
dotenv.config() //reads .env file and appends stuff to process.env at runtime
Now say PM2 launches MYVAR=hey; node app.js and then inside app.js we run dotenv.config() that reads an .env file containing MYVAR=foo. Which var will be in process.env?
ecosystem.config.js
{
//...standard pm2 config above
env: {
MYVAR: 'ecosystem',
},
}
.env/dotenv
MYVAR=dotenv
Code
dotenv.config()
console.log(process.env.MYVAR)
dotenv.config() will not overwrite variables if it sees they already exist in the process.env (that they've been assigned the PM2 MYVAR=foo; node app.js way.
So process envs set before launch will take precedence.
This is actually in the README of dotenv.
What happens to environment variables that were already set?
We will never modify any environment variables that have already been set. In particular, if there is a variable in your .env file which collides with one that already exists in your environment, then that variable will be skipped. This behavior allows you to override all .env configurations with a machine-specific environment, although it is not recommended.
https://www.npmjs.com/package/dotenv#what-happens-to-environment-variables-that-were-already-set
If you absolutely need to override existing env vars - use the dotenv-override package.

Read from file into config

I have an ember project. The root folder has a file called version which just says 1.1.0
In the root folder I also have my client folder (ember project) with a config folder and then environment.js for my app variables.
Im trying to read from the version file and add its contents in the environment.js file as a varaible.
Im currently trying like this: version: $.getJSON("../../VERSION")
but im getting the unexpected identifier error. With Node I would use: version: fs.readFileSync(__dirname + '/../VERSION').toString().trim(),
How would I do this with ember? thanks
You'll need to get the version in environment.js and expose it as an ENV key-value pair in the ENV hash/object.
environment.js is the only[1] file with access to things outside of the frontend / browser environment.
Once you have your ENV saying what version you have (maybe via fs), you can then import the enviroment via import ENV from 'app-name/config/environment', and access your version via ENV.versionPropertyThingThatYouMade
hope this helps!
[1] there are others, but that's not important right now

Reading environment variables from pug

I'm using pug to compile static html. My own static site generator, kinda.
I have no node.js server code besides this line in my package.json file: "watch-pages": "pug -O options.json -w pages/ --out _static-website/"
But, I need to read environment variables like NODE_ENV inside of pug templates. How might I do this?
This is fairly simple; you may find another way to do it but what I tried (successfully) was to simply define a .js file to pass as the options parameter which includes the variables I wanted. For example:
// env.js
module.exports = { env: process.env };
Then the template can be something like:
// tmp.pug
ul
each e in env
li=e
And you can then run pug -O env.js tmp.html and it will create a env.html with the environment variables rendered as list items.

Categories

Resources