How to Set .env with JS - javascript

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

Related

In Next.js, Is there a way to change the location of the .env file to be outside of the app's folder?

I'm making a project that contains a Next.js app and my .env file is located in the project's root.
How do I make Next.js look for the .env file anywhere except the app's root folder?
I already tried using the dotenv package, but unfortunately, that doesn't work.
In your next.config.js file configure the dotenv package as this.
const path = require('path');
const dotenv = require('dotenv');
dotenv.config({ path: path.join(__dirname, '/path/to/.env') });
To change the location of the .env file, install the 'dotenv' package and add it to your next.config.js file:
dotenv.config({path: '../.env'})
(In my case the path is ../.env to go one folder up)
To be able to access the environment variables on the client side declare them in the next.config.js file like that:
const nextConfig = {
...
//to be able to use the environment variables on the client side
//==============================================================
env: {
EXMAPLE_ENVIRONMENT_VARIABLE: process.env.EXMAPLE_ENVIRONMENT_VARIABLE,
},
...
}
module.exports = nextConfig;

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

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.

Node js, process.env not reading enviroment variables

Even though I can see that an environment variable was created on windows, process.env always returns undefined. I did set all my variables, and when I check them manually, they all appear in the prompt, but the process.env always stays undefined.
P.S. I don't have admin privileges, except when I check the process.env.NODE_ENV.
You need to read them first.
Use the dotenv package.
Install:
npm install dotenv
In you project code:
require('dotenv').config()
Add .env file in you project folder like this:
DB_HOST=localhost
DB_USER=root
DB_PASS=s1mpl3
Try get env var like this:
const db = require('db')
db.connect({
host: process.env.DB_HOST,
username: process.env.DB_USER,
password: process.env.DB_PASS
})
By the way, it is not enough to write them to a file in order for them to become environment variables. You need to write them in the console and then they become environment variables. The .env file method lets you write them to a file and read them from there through the donenv package.

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.

Categories

Resources