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

时间:2019-06-20 19:41:45

标签: javascript node.js reactjs environment-variables next.js

我读过here时,React确实支持环境变量,只要它们以REACT_APP_为前缀。但是,我在开发时需要它们。我如何获得这种支持?

注意:我正在使用Next.js

1 个答案:

答案 0 :(得分:2)

我确定以前已经回答过,但是它对您链接的内容说的很对。在项目的根目录(与package.json,.gitignore等级别)中创建2个文件,.env.development和.env.production。根据下面列出的层次结构,无论运行哪种脚本,都可以确定使用哪种脚本。

.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)
相关问题