简体   繁体   English

React-开发和生产中的环境变量

[英]React - Env variables in development AND production

As I've read here , React does support environment variables as long as they are prefixed with REACT_APP_ . 正如我在这里阅读的,React确实支持环境变量,只要它们以REACT_APP_为前缀。 However, I need them in development and production. 但是,在开发生产中我需要它们。 How can I get this support? 我如何获得此支持?

Note: I'm using Next.js 注意:我正在使用Next.js

I'm sure this has been answered before, but it says it right on what you linked. 我敢肯定这已经被回答过了,但是它对您链接的内容说的很对。 Create 2 files, .env.development and .env.production in the root of your project (same level as package.json, .gitignore, etc). 在项目的根目录(与package.json,.gitignore等级别)中创建2个文件,.env.development和.env.production。 Whichever script you run determines which one gets used, in accordance with the hierarchy listed below. 根据下面列出的层次结构,无论运行哪种脚本,都将确定使用哪种脚本。

.env: Default.
.env.local: Local overrides. This file is loaded for all environments except test.
.env.development, .env.test, .env.production: Environment-specific settings.
.env.development.local, .env.test.local, .env.production.local: Local overrides of environment-specific settings.


Files on the left have more priority than files on the right:

npm start: .env.development.local, .env.development, .env.local, .env
npm run build: .env.production.local, .env.production, .env.local, .env
npm test: .env.test.local, .env.test, .env (note .env.local is missing)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM