I want to load environment variables into the general config of my Express app. Locally I work with a .env file, but when deploying, I'll inject the env vars into the docker container.
So, I've got a default.json with some hard-coded config values. Then I've got the custom-environment-variables.json file like so:
"app": {
"nodeEnv": "NODE_ENV",
"port": "PORT",
"hostname": "HOSTNAME"
}
"mongoDb": {
"connectionString": "MONGODB_CONNECTION_STRING"
},
"redis": {
"connectionString": "REDIS_CONNECTION_STRING"
},
"logDna": {
"key": "LOGDNA_KEY"
}
and a .env file within the root dir of my project.
In server initiating file I've got:
import * as dotenv from 'dotenv';
dotenv.config();
import * as config from 'config';
console.log(process.env);
console.log(config);
I also tried placing them in a normal order:
import * as config from 'config';
import * as dotenv from 'dotenv';
dotenv.config();
Another thing I tried is to separate the app declaration from the services init and app start. Like so in app.ts:
import * as App from 'express';
import * as dotenv from 'dotenv';
dotenv.config();
console.log(process.env);
const app = new App();
export app;
And then in index.ts:
import app from './app';
import * as config from 'config';
console.log(config);
// app.use() and other inits
app.listen(...);
But to no avail. The .env file is loaded, I can see the vars in process.env, but they do not get loaded by the config in custom-environment-variables.json. F.e. the nodeEnv, port and hostname variables are loaded. But they are loaded into the container before the application is started.
This brings me to the thought that the config loads before the dotenv module manages to load the values from the .env file. Essentially, it seems like the problem will only be existent in DEV env, since it's the only place I'll be loading environment variables from .env file. For any other environment, they'll be present in the container environment before the app even starts.
Can I maybe force re-load config?
Any suggestions how to achieve that?
Eventually I settled for external loading of the local env vars, alongside the node process startup:
node --require=dotenv/config src/index.js
inspiration from here.
Related
I have a project that has a helper folder filled with helper functions or axios instances and i need to access my env files there.
ConfigModule.forRoot({ envFilePath: envFiles, isGlobal: true})
This works inside all modules but i cant use process.env in non module files.
just install dotenv 'npm install dotenv --save' to your dependencies and then add to your non module files code bellow, this will load your data from env.
documentation: https://www.npmjs.com/package/dotenv
import { config } from 'dotenv';
config();
const data = process.env.SOME_DATE;
I am using nestjs for my nodejs project, and I want to load the .env file from outside of the project.
Here is the directory structure
.env
services
/nodejs
/my-apis
/src
/main.ts
and in my main.ts
import * as dotenv from "dotenv";
dotenv.config({ path: `../../../../.env` });
console.log("process.env.PORT", process.env.PORT);
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.setGlobalPrefix("/api");
await app.listen(process.env.PORT);
}
but it is not getting the values from .env, what can be the issue?
https://stackoverflow.com/a/42335383/10562569
require('dotenv').config({path:__dirname+'/./../../.env'})
This might help. Customize it to your need.
I was loading the env file in main.ts. My app.module.ts was using some env variable to initialize Mongo.
So In my app.module.ts env variable was undefined. So I loaded the env in app.module.ts instead of main.ts
Also I used __dir_name for path
import * as dotenv from "dotenv";
dotenv.config({ path: `${__dirname}/../../../../.env` });
and this worked.
Currently, I have public and private .pem files that I import into my Express app. How can I provide those in my env files? Since the keys span multiple lines, I'm unable to import from env properly. I guess it's a question on how to import keys from env so that in the end I just have to do process.env.pub_key.
If you are using a .env fil to manage your environment variables, I would use the dotenv package and then export your variables from a config file.
.env
PUB_KEY_PATH="./keys/public.pub/rsa-abc-123-lmnop"
SOME_OTHER_ENV_VAR="some-other-value"
config.js/ts
import dotenv from 'dotenv'
dotenv.config()
export const PUB_KEY_PATH = process.env.PUB_KEY_PATH
usage
import { PUB_KEY_PATH } from './config'
console.log(PUB_KEY_PATH)
I'm using the dotEnv package for my Node/Express server. I have the .env file in the root of my project. In the root I have a src folder where I import the package in my app.js file which is the entry point of my server like so: (root/src/app.js)
import dotenv from "dotenv";
dotenv.config();
This works fine and I can access it in the app.js without issue.
But I have a file called config.js in root/src/config that I also want to use the env variables. But when I use something like process.env.DB_HOST I get undefined. How can I use these env variables in this file (and subsequent files)? I managed to get it to work by using
require("dotenv").config({ path: `${__dirname}/../../.env` });
Is there an easier way to access the env variables without having to import the package with the correct path to the env in each file I want to use it in?
My project structure look like this:
|-project
|-src
|-index.ts
|-.env
in index.ts I'm trying to load environment variables:
import dotenv from 'dotenv';
dotenv.config();
I've also tried with
dotenv.config({
path: __dirname+"/../.env"
});
file .env itself is surely ok,
where is the mistake? Thank for help
command that I'm running is ts-node ./src/index.ts
edit: I'm accesing values using expression like process.env.DB_CONNECT
.env file
DB_CONNECT=someValue
TOKEN_KEY=someValue
Can you try this?
import { resolve } from 'path';
import dotenv from 'dotenv';
dotenv.config({ path: resolve(__dirname, "../.env") });
nevermind, it was my bad implementation of default values in functions.
closing