在我的React项目中想要通过
.env
配置端口启动生产环境。
这是我的
.env
文件:
PORT = 3001
APP_BE_IP = 127.0.0.1
APP_BE_PORT = 3333
REACT_APP_API_KEY = 'http://${APP_BE_IP}:${APP_BE_PORT}/'
API_TIMEOUT_MS = 10000
dotenv-cli
{
... ... ...
... ... ...
"scripts": {
"envport": "dotenv -p PORT",
"start:prod1": "serve -s build -l ${dotenv -p PORT}", // Need to Configure with env.PORT
"start:prod": "serve -s build 3001",
"build:prod": "react-scripts build",
"prod": "npm run build:prod && npm run start:prod"
... ... ...
}
"dependencies": {
... ... ...
"react": "^18.3.1",
... ... ...
},
"devDependencies": {
"dotenv-cli": "^7.4.2",
... ... ...
},
"peerDependencies": {
"react": "^18.3.1",
"react-dom": "^18.3.1"
},
... ... ...
}
运行时
npm run envport
它返回正确的 env.PORT
值 (3001)。npm run start:prod
脚本中使用它?
如果我使用
$PORT
,它可以与 dotenv-cli
完美配合。{
... ... ...
... ... ...
"scripts": {
"start:prod": "dotenv -e .env serve -s build -l $PORT",
"build:prod": "react-scripts build",
"prod": "npm run build:prod && npm run start:prod"
... ... ...
}
... ... ...
... ... ...
}