为什么我的环境变量无法在create React应用程序中获取? [英] Why my environnement variables not picked up with create React app?

查看:463
本文介绍了为什么我的环境变量无法在create React应用程序中获取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在WSL上创建的react应用程序中使用环境变量.我无法从我的应用访问变量. 这是我所做的:

I am trying to use environment variables in a react app created on WSL. I cannot access the variables from my app. Here is what I did:

  1. 在我的应用程序的根文件夹(在Rails应用程序的客户端文件夹内)中创建一个.env文件
  2. 创建变量:REACT_APP_API_URL=http://localhost:3000/api/v1/
  3. 在控制台中记录变量:console.log(process.env.REACT_APP_API_URL)
  4. 重新启动服务器
  1. Create a .env file at the root folder of my react app (within a client folder of a Rails app)
  2. Create a variable: REACT_APP_API_URL=http://localhost:3000/api/v1/
  3. Log the variable in my console: console.log(process.env.REACT_APP_API_URL)
  4. Restarted the server

我不断得到一个未定义的变量.

I keep getting an undefined variable.

我可能缺少与WSL相关的任何内容吗?我有点头绪,因为我无法在网上找到类似的问题.

Am I missing anything related to WSL maybe? I am a bit clueless as I cannot find a similar problem online.

谢谢!

推荐答案

我认为您的文件位于错误的目录中.请参阅此处:在VueJs应用程序中始终未定义环境变量(您的问题可能重复).

I think your file is in the wrong directory. See here: Environment variables are always undefined in VueJs application (possible duplicate of your question).

这篇关于为什么我的环境变量无法在create React应用程序中获取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆