• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

已解决vue/react .env.development 读取不到变量undefined

武飞扬头像
程序边界
帮助5


一、问题

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
  • build 读取 .env 中变量
  • start 读取 .env.development 中变量

三、拓展学习

Adding Custom Environment Variables | Create React App

  • 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, .env
npm run build: .env.production.local, .env.local, .env.production, .env
npm 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.


这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfieakh
系列文章
更多 icon
同类精品
更多 icon
继续加载