how can i use .env file from my root directory? - environment-variables

I am working in a MERN project. I've a .env file in my root directory. I want to use some keys from that file on react app. can I use that .env file or should I make a new .env file?

here is the library "dotenv" that makes it,
also have wrap for webpack "dotenv-webpack"
https://www.npmjs.com/package/dotenv

Related

Can not Access variables of .env file

In my React app, I was trying to access some variables from .env.local but every time I print It it shows undefined.
// src/.env.local
REACT_APP_SECRET_CODE=123456
// In different component
console.log(process.env.REACT_APP_SECRET_CODE);
output: undefined
Note: I tried to use .env file as well, but did not work.
React Script version is 5.0.1
try this ,
make sure your .env file is in root folder and install dotenv package by using, npm install dotenv

Prevent the .env file being copied to a Docker container

i'm newbie in Docker and i'm trying to make a "Starter-Boilerplate" with express and mongodb,
but i don't want the .env file in my docker image/containers, i've tried with .dockerignore file but the .env file still being copied inside the container in all docker-compose builds.
My .dockerignore file:
node_modules
.env
Link of the repo to reproduce the case:
https://github.com/josuerodcat90/expressjs-server-starter/tree/docker
Any idea or suggestion?, thanks in advance mates.
I found a solution for these cases when you need to keep the docker volumes without troubles with your .env file and your docker-compose rules, see below:
First i declare 2 new variables in my .env file:
ENV=DOCKER //this can switch between DOCKER and LOCAL
DOCKER_PORT=3000 //for example
and then i've made a little change in my index.js and database.js files, when im working in Docker the enviroment vars must be different of my Local vars, and then the problem solves.
Thanks to all for your help and interest in my issue, sorry for my bad english haha.
Create a .dockerignore file if you don't already have one and add **/*.env inside it. This will prevent to copy any .env file from any folder.

docker-compose project root reference

I have a docker-compose.yml file at the root of my project, and within that project there are several modules in sub-directories.
I find that I can issue docker-compose commands from anywhere within the project root and these work, compose seems to search backwards until it finds the docker-compose file. This is useful.
However, it does not locate the .env file in the project root, it seems to want to find the file in the current folder so docker-compose up commands don't work and I imagine volume mounts that reference folders relative to the docker-compose file may not work either.
How can I change my docker-compose file to such that these references refer to the a location relative to the docker-compose.yml file itself?

How are Environment Variables used with Gatsby?

In the context of GatsbyJS, how are environment variables supposed to be used? I've read and re-read the official docs but I'm still confused.
Specifically:
Does Gatsby automatically read the .env.development file when doing gatsby develop?
Similarly, does it ignore the .env.development file and only read the .env.production when doing gatsby build?
Are you supposed to add .env.development and .env.production to .gitignore?
Are you supposed to manually copy (e.g. via scp) the .env.* files to your servers?
I used .env.development and .env.production files, but didn't need dotenv because I wasn't trying to get these env variables into node.js. I wanted these environment variables in JS files.
Q1) Yes. If you make an .env.development file in the root directory, add this one line of code IMG_URL=https://picsum.photos/200/300?image=1079, and then in your JS add this line of code <img src={'${process.env.IMG_URL}'} alt="" /> in a render(){} function in one of your react components. Then gatsby develop, you'll see the image
Q2) Yes it appears to. I created .env.development and .env.production files, defined a variable in the development one, then deployed to production. The env variable wasn't defined in the .env.production file.
Q3) hmm this probably depends on your repo. If it's a public or private. As well as the contents of the file. If you're using the .env files for API URLs etc.. that's fine, but Secrets shouldn't be in the .env files or the repo.
Q4) I'd say that committing and deploying are generally different. Unless you have a setup with auto deployments on commit. I have added my files to my .gitignore then copy all my local files to my live server. Maybe someone else has a better answer to this one.
Maybe this approach might be better for you alternative config go to oliverbenns comment

aws code deploy: No such file or directory - appspec.yml

When trying to deploy my rails app through code deploy I get the following error message:
No such file or directory - /opt/codedeploy-agent/deployment-root/b3ff73b4-aa93-4e49-99e4-c26cdcf7a6f0/d-06LE313R9/deployment-archive/appspec.yml
on a "BeforeInstall" error.
I have appspec.yml in my rails app root directory (e.g it is myapp/appspec.yml), and have no idea how to put the appspec file into the deployment-archive directory.
The appspec.yml file should be at the root of the archive you upload to S3 or the GitHub repo you are deploying with. Try moving your appspec up one directory.
\
|-- appspec.yml
`-- myapp
`--- (the rest of your rails app source tree)
Note that the appspec mostly uses paths relative to the archive root, so you might have to adjust them if you move your appspec.
Alternatively, you can create your bundle from inside your myapp directory so that the appspec.yml is still in the root directory of your archive.

Resources