I've been seeing a lot of NodeJS and dotenv tutorials and articles where they define an ENV_MODE=development variable within the config.env file.
But require('express').get('env') already gives us the environment express is set on running. Why not just use the express env variable to check the environment mode? Why do we need to also set the explicit variable in the .env file?
You have to set the environment variable somewhere.
Doing it in a .env file ties it to a specific computer, which means you don't have to either:
Remember to set it manually before you run the program
Bind it into package.json and risk running the wrong command when you run the code on staging/qa/production
… and it lets you keep it with any other environment variables you have (such as API keys) which shouldn't be committed to version control (since they are secrets).
Related
I'm trying to move all the sensitive variables to .env file. It works just fine server side on Node but I'm having trouble doing it with React.
I referenced these answers: this and that
1.I changed my .env so it looks like this
REACT_APP_SIGNER_PK = xxxxxx-xxxxx-xxxxxx
Added the variable itself to the file containing script
const REACT_APP_SIGNER_PK = process.env.REACT_APP_SIGNER_PK;
Placed .env inside of my root folder
I restart the server after the changes with yarn run dev
To test out the changes I put this print statement from the file where I put the env variable in
console.log(REACT_APP_SIGNER_PK);
On the restart of the local server I check the console and in the terminal where the server is running I see the print statement with my environmental variable, but in the browser console I see this print as undefined
The only thing that differs in my action from the references is that I use yarn run other than npm start
What did I do wrong?
Thanks to the comment from #OFRBG I managed to find a solution.
Adding REACT_APP_ app to the my .env variables did not work because I was using nextJS instead of Create-React-App.
In order to fix the problem this problem for nextJS users add NEXT_PUBLIC_ instead as a prefix for your .env variables
I am using next js as the basis for my application. My environment variables work as long as they are read within the scope of next js but I need to use them inside a script that does not use next js.
With dotenv we can do the following:
require('dotenv').config();
Which parses the environment config files and allows you to get the variables from process.env.
Because I am using next js I would prefer to use their environment variable implementation instead of adding a second way to store environment variables.
Is there an equivalent of require('dotenv').config(); for next js or another way to load the environment variables outside the scope of next js?
Ok I went into the git repo of next js and found a way to do it.
You have to install #next/env and then run require('#next/env').loadEnvConfig('./');
In my react app, I have a component say chart in which an external API is called.
While running in the local, the API URL will be localhost:8080
When deployed and the API URL should be prod:8080
The examples based on .env uses process.env and inside the component where the API is called process is undefined.
Is there a way to access the env file inside the component which runs on the browser.
If you are using CRA:
React's documentation on environment variables:
"Your project can consume variables declared in your environment as if they were declared locally in your JS files. By default you will have NODE_ENV defined for you, and any other environment variables starting with REACT_APP_."
Otherwise you might use something like dotenv and import it in your application index.tsx
For your specific use case, you also want to have 2 .env files since you want one for development and production. So take a look a this.
I'm familiar with gulp and the ability to have a distinct configuration for each environment within a single configuration file. That way running gulp dev would allow you to start your server with the configuration of your dev environment, gulp staging for your staging and gulp prod fro production.
I'm now looking at restify and am trying to determine if something similar can be done with it. I've tried researching this online and haven't found anything meaningful. Is this possible and if so could somebody provide an example?
You can use dotenv package to load different configuration file. For example
.env.dev For Development environment
.env.prod for Production environment
.env.test for Testing environment
you can import file according to NODE_ENV var
or you can simply add all configuration variable in one file for example
.conf.env and import it.
Stuck with Bitbucket pipelines.
In my code i have some secret info
apiKey: process.env.apiKey,
authDomain: process.env.authDomain
But bitbucket pipeline during build can't access them.
Have set up variables via Bitbucket Env variables UI
In yml file i can access variables via $Variable, but how to do this with code?
Thanks
You can set up environment variables in Bitbucket, and access them via $variableName in the YAML build file.
Bitbucket uses the environment variables recorded in Bitbucket when running your build pipeline. But it only does this inside your YAML build file. It won't do anything with environment variables referenced in the rest of your code.
For the rest of your codebase, environment variables are worked out on the hosting server at runtime. So if you use environment variables elsewhere in your code (e.g. in your app.js), you need to configure these in your hosting environment.
This should provide more context for your use case - bitbucket docummentation
Create the environment variable in your env file
use $ENV_NAME to access your specified variable