I want to use the current given local IP address in the React Native project.
So I created a Powershell script file that finds the IP address and saves it into the system variable$env:IPADDR
.
Write-Host "Getting current IP Address" $env:IPADDR = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration | where {$_.DHCPEnabled -ne $null -and $_.DefaultIPGateway -ne $null}).IPAddress | Select-Object -First 1 Write-Host " ---->" $env:IPADDR
Now I want to pass this variable in my project's.env
file.
While$npm_package_name
works fine,$env:IPADDR
doesn't seem to work. The output result is not the evaluation of the previously defined environment variable, but the variable literal text itself, that is,console.log(REACT_APP_API_URL) --> http://$env:IPADDR:3000/
instead of the result of the evaluationhttp://192.168.10.4:3000/
.
My.env
file is created like this.
REACT_APP_API_URL=http://$env:IPADDR:3000/ REACT_APP_NAME=$npm_package_name
So, what did I do wrong? How do I dynamically evaluateREACT_APP_API_URL
using the PowerShell environment variable$env:IPADDR
?
I discovered that Windows environment variables cannot be expanded natively in the
.env
file. Instead, I extended the PowerShell script to "replace" the required variables by working directly in the.env
file.This is the final
run.ps1
PowerShellscript file.