无法使用 create-react-app 设置环境
Cannot set env with create-react-app
我使用 create-react-app
和 react-script
版本 1.0.7
, node 6.10.2
在我的项目根目录中,我创建了 .env
:
API_ENDPOINT=https://dev-api.com
在 src/common/api.js
中,我有:
import axios from 'axios';
const apiEndpoint = process.env.API_ENDPOINT;
axios.defaults.baseURL = apiEndpoint || 'https://prod-api.com';
使用 yarn run build
构建项目后,我从浏览器控制台检查 api 调用,它始终是 https://prod-api.com
。
你有什么想法吗?谢谢
我刚刚又看了一遍docs。
环境变量应以 REACT_APP_
:
开头
By default you will have NODE_ENV defined for you, and any other
environment variables starting with REACT_APP_
我使用 create-react-app
和 react-script
版本 1.0.7
, node 6.10.2
在我的项目根目录中,我创建了 .env
:
API_ENDPOINT=https://dev-api.com
在 src/common/api.js
中,我有:
import axios from 'axios';
const apiEndpoint = process.env.API_ENDPOINT;
axios.defaults.baseURL = apiEndpoint || 'https://prod-api.com';
使用 yarn run build
构建项目后,我从浏览器控制台检查 api 调用,它始终是 https://prod-api.com
。
你有什么想法吗?谢谢
我刚刚又看了一遍docs。
环境变量应以 REACT_APP_
:
By default you will have NODE_ENV defined for you, and any other environment variables starting with REACT_APP_