最佳答案
我在创建反应应用程序中使用了以下环境变量:
console.log(process.env.REACT_APP_API_URL) // http://localhost:5555
当我通过读取 .env
文件来运行 npm start
时,它会工作:
REACT_APP_API_URL=http://localhost:5555
How do I set a different value like http://localhost:1234
when executing a npm run build
?
这是我的 package.json
文件:
{
"name": "webapp",
"version": "0.1.0",
"private": true,
"devDependencies": {
"react-scripts": "0.9.0"
},
"dependencies": {
"react": "^15.4.2",
"react-dom": "^15.4.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}