React - 开发和生产中的 Env 变量
React - Env variables in development AND production
据我了解 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)
据我了解 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)