如果你的 React 项目不是使用 create-react-app 构建的,也没有后端,那么设置 ENV 变量的过程会是什么?
如果你的 React 项目不是使用 create-react-app 构建的,也没有后端,那么设置 ENV 变量的过程会是什么?
找到了答案。引用来自这篇文章,作者是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
yarn add dotenv-webpack
const Dotenv = require('dotenv-webpack');
module.exports = {
...
plugins: [
new Dotenv()
]
...
};
URL=something.com
我认为你可以在Linux中使用.bashrc文件添加任何想要在程序中使用的环境变量。
Variable=<value>
您可以添加变量和相应的值,然后保存并使用source abc.bashrc命令来源化bashrc文件,这样这些变量就会在当前终端的环境中可用。 您可以在任何可以读取环境变量的编程语言中使用此文件。
REACT_APP_...
)。你可能不想在前端捆绑包中包含每个变量。 - timotgl