Setting up ENV Variables Without create-react-app

别等时光非礼了梦想. 提交于 2021-01-23 06:36:27

问题


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?


回答1:


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




回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/59243719/setting-up-env-variables-without-create-react-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!