简体   繁体   English

为什么反应看不到.env

[英]Why react doesn't see .env

I have .env which has REACT_APP_API_URL = http://localhost:5000/ .我有.env它有REACT_APP_API_URL = http://localhost:5000/ I use it in this code snippet我在这个代码片段中使用它

import axios from "axios";

const host = axios.create({
    baseURL: process.env.REACT_APP_API_URL
});

const authHost = axios.create({
    baseURL: process.env.REACT_APP_API_URL
});

When I try to output console.log(process.env.REACT_APP_API_URL);当我尝试 output console.log(process.env.REACT_APP_API_URL); then the value is displayed undefined, why is that?然后显示的值未定义,这是为什么呢? The .env file is located in the directory where all client files are present .env文件位于所有客户端文件所在的目录中

first install dotenv: npm i dotenv
after that import this to file: const dotenv = require("dotenv")
and config dot env: dotenv.config()

Please check your .gitignore file, sometimes the create-react-app adds .env file to the.gitignore entry.请检查您的.gitignore文件,有时create-react-app会将.env文件添加到 .gitignore 条目。 Maybe that's the reason.也许这就是原因。

Did you try the dotenv package ?您是否尝试过dotenv package

npm install dotenv

In your config file add:在您的配置文件中添加:

require("dotenv").config();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM