据我读过here ,React确实支持环境变量,只要它们以REACT_APP_
为前缀。但是,我在开发和生产中需要它们。我怎样才能获得这种支持?
注意:我使用的是 Next.js
最佳答案
我确信这个问题以前已经得到过回答,但它在您链接的内容上说得对。在项目的根目录中创建 2 个文件,.env.development 和 .env.Production(与 package.json、.gitignore 等级别相同)。根据下面列出的层次结构,无论您运行哪个脚本,都将决定使用哪个脚本。
.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)
关于javascript - React - 开发和生产中的环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56692876/