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('./');
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'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).
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 created a CLI in node.js. For each command the user passes in command line arguments such as environment and tenant. I want to set variables that can be accessed by any other file in the application.
I am using yargs so each one of my CLI commands takes in arguments and calls a function. Before calling the function I would like to set some global variables that the rest of the js files will use.
I realized I can use environment variables for this since you can set them in the code as well.
There is method which I need to run just once when the node application is started ,I think about to put it in the server.js which is my executable file to the application but Im not sure that this is the right way to go,which other option I can use in this case?
Depending on when specifically it's required to be run, it may be suitable to put the function into a separate javascript file, and execute it as part of your npm start script within your package.json?
Based on your comment below stating it is a child process. It would be appropriate to execute it from the server file, but best practice would be to abstract the function out into a separate file, require it into your server file and then call it.