【已解决】vue/react .env.development 中读取不到变量:undefined
中读取不到变量,undefined。react 项目中,通过。
一、问题
react 项目中,通过 ·process.env. 在 .env 或 .env.development 中读取不到变量,undefined
二、解决
- 首先,确保
.env和.env.development文件放在项目根目录(和package.json同级),而不是src目录 - 其次,必须以
REACT_APP_开头建立自定义环境变量(同理,vue项目是以VUE_APP_开头)- bad:
API_URL=http://localhost:8080 - good:
REACT_APP_API_URL=http://localhost:8080
- bad:
- build 读取
.env中变量- start 读取
.env.development中变量
三、拓展学习
- What other .env files can be used?
Note: this feature is available with react-scripts@1.0.0 and higher.
.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.local, .env.development, .envnpm run build: .env.production.local, .env.local, .env.production, .envnpm test: .env.test.local, .env.test, .env (note .env.local is missing)
These variables will act as the defaults if the machine does not explicitly set them.
更多推荐



所有评论(0)