In my react project want to start production environment through .env
config port.
This is my .env
file:
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
I have add dotenv-cli
in package.json
{
... ... ...
... ... ...
"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"
},
... ... ...
}
when run npm run envport
it's returning correct env.PORT
value (3001).
But how to use that in npm run start:prod
script ?
dotenv-cli
After lots of research found solution EOD.
And My final script is :
{
... ... ...
... ... ...
"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"
... ... ...
}
... ... ...
... ... ...
}