简体   繁体   中英

Cannot set env with create-react-app

I use create-react-app with react-script version 1.0.7 , node 6.10.2

In my project root, I create .env :

API_ENDPOINT=https://dev-api.com

And in src/common/api.js , I have:

import axios from 'axios';

const apiEndpoint = process.env.API_ENDPOINT;
axios.defaults.baseURL = apiEndpoint || 'https://prod-api.com';

After building the project with yarn run build , I check the api call from browser console and it always is https://prod-api.com .

Do you have any ideas. Thanks

I just read the docs again. Environment variable should start with REACT_APP_ :

By default you will have NODE_ENV defined for you, and any other environment variables starting with REACT_APP_

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.

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