Reputation: 806
What would be the process of setting up ENV variables to work in your react project when your react project isn't built using create-react-app, and has no backend?
Upvotes: 8
Views: 5931
Reputation: 101
Step 1:
yarn add dotenv-webpack
Step 2: on webpack.config.js file:
const Dotenv = require('dotenv-webpack');
module.exports = {
...
plugins: [
new Dotenv()
]
...
};
Last step: create .env file & put system variable in it:
URL=something.com
Upvotes: 1
Reputation: 806
Found the answer. Quoted from this post by Aminu Kano.
Webpack Users
If you are using webpack, you can install and use dotenv-webpack plugin, to do that follow steps below:
Install the package
yarn add dotenv-webpack OR npm i dotenv-webpack
// .env API_KEY='my secret api key' Add it to webpack.config.js file
// webpack.config.js const Dotenv = require('dotenv-webpack');
// webpack.config.js const Dotenv = require('dotenv-webpack'); module.exports = { ... plugins: [ new Dotenv() ] ... };
Use it in your code as
process.env.API_KEY
For more information and configuration information, visit here
Upvotes: 9
Reputation: 116
I think you can you a .bashrc file in linux to add any environment variable you want to use in the program.
Variable=<value>
you can just add variables and corresponding values and then you save and source the bashrc file using source abc.bashrc and then those variables will be available in the envrionment for the current terminal. You can use this file in any programming language where you can read the environment variables.
Upvotes: 0