I have .env
which has 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);
then the value is displayed undefined, why is that? The .env
file is located in the directory where all client files are present
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. Maybe that's the reason.
Did you try the dotenv package ?
npm install dotenv
In your config file add:
require("dotenv").config();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.