在React Native的`.env`文件中设置Windows PowerShell环境变量?

huangapple go评论77阅读模式
英文:

Windows powershell environment variable in react-native .env file?

问题

我想在React Native项目中使用当前给定的本地IP地址。

为此,我创建了一个PowerShell脚本文件,用于查找并将IP地址保存到系统变量$env:IPADDR中。

Write-Host "获取当前IP地址"
$env:IPADDR = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration | where {$_.DHCPEnabled -ne $null -and $_.DefaultIPGateway -ne $null}).IPAddress | Select-Object -First 1
Write-Host "    ---->" $env:IPADDR

现在我想在我的项目的.env文件中传递这个变量。

尽管$npm_package_name 工作正常,但$env:IPADDR 似乎不起作用。与评估先前定义的环境变量不同,输出结果是变量文字本身,例如 console.log(REACT_APP_API_URL) --> http://$env:IPADDR:3000/,而不是评估结果 http://192.168.10.4:3000/

我的.env文件是这样创建的。

REACT_APP_API_URL=http://$env:IPADDR:3000/
REACT_APP_NAME=$npm_package_name

那么,我在哪里出错了?如何使用PowerShell环境变量$env:IPADDR来动态评估REACT_APP_API_URL

英文:

I want to use the currently given local IP address inside a React Native project.

For this reason, I created a Powershell script file that finds and save the IP Address to 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

在React Native的`.env`文件中设置Windows PowerShell环境变量?

Now I want to pass this variable inside my project's .env file.

While the $npm_package_name works fine, the $env:IPADDR does not seem to work. Instead of evaluating the previously defined environmental variable, the output result is the variable literal text itself ie console.log(REACT_APP_API_URL) --> http://$env:IPADDR:3000/ instead of the evaluated result http://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, where am I doing wrong here? How can I dynamically evaluate the REACT_APP_API_URL using the PowerShell environmental variable $env:IPADDR?

答案1

得分: 1

<!-- language-all: sh -->

我发现在 `.env` 文件中原生地扩展 Windows 环境变量的方法。相反,我扩展了 PowerShell 脚本,以在 `.env` 文件内直接工作来“替换”所需的变量。

这是最终的 `run.ps1` *PowerShell* 脚本文件。

#查找本地 IP 地址并将其保存到 $env:IPADDR 变量中
Write-Host "获取当前 IP 地址"
$env:IPADDR = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration | where {$_.DHCPEnabled -ne $null -and $_.DefaultIPGateway -ne $null}).IPAddress | Select-Object -First 1
Write-Host "    ---->" $env:IPADDR

#使用新的服务器地址 http://xxx.xxx.xxx.xxx:3000/ 替换键 [REACT_APP_API_URL] 的值
$regex = '(?<=REACT_APP_API_URL=)[^=]*'
$file = '.env'
$addr = 'http://' + $env:IPADDR + ':3000/'
(Get-Content $file) -replace $regex, $addr | Set-Content $file

#启动 NPM 脚本
npm run start
英文:

<!-- language-all: sh -->

I found no way to expand Windows environmental variables natively inside .env file. Instead, I extended the PowerShell script, to "replace" the desired variable by working directly inside the .env file.

Here is the final run.ps1 PowerShell script file.

#Find local IP addr and save it to $env:IPADDR variable
Write-Host &quot;Getting current IP Address&quot;
$env:IPADDR = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration | where {$_.DHCPEnabled -ne $null -and $_.DefaultIPGateway -ne $null}).IPAddress | Select-Object -First 1
Write-Host &quot;    ----&gt;&quot; $env:IPADDR

#Replace the value of the key [REACT_APP_API_URL] with the new server ie http://xxx.xxx.xxx.xxx:3000/
$regex = &#39;(?&lt;=REACT_APP_API_URL=)[^=]*&#39;
$file = &#39;.env&#39;
$addr = &#39;http://&#39; + $env:IPADDR + &#39;:3000/&#39;
(Get-Content $file) -replace $regex, $addr | Set-Content $file

#Start NPM script
npm run start

huangapple
  • 本文由 发表于 2023年7月12日 21:46:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76671288.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定