.NET SpaProxyLaunchCommand 不支援 pnpm 解決辦法
.NET
專案如果用 SpaProxyLaunchCommand
搭配上 pnpm
會出現錯誤的狀況,
設定檔:
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>default</LangVersion>
<!-- SPA settings -->
<SpaRoot>ClientApp\</SpaRoot>
<DefaultItemExcludes>$(DefaultItemExcludes);$(SpaRoot)node_modules\**</DefaultItemExcludes>
<SpaProxyServerUrl>http://localhost:9000</SpaProxyServerUrl>
<SpaProxyLaunchCommand>pnpm dev</SpaProxyLaunchCommand>
會出現以下錯誤:
info: Microsoft.AspNetCore.SpaProxy.SpaProxyLaunchManager[0]
No SPA development server running at https://localhost:5173 found.
fail: Microsoft.AspNetCore.SpaProxy.SpaProxyLaunchManager[0]
Failed to launch the SPA development server 'pnpm dev'.
System.ComponentModel.Win32Exception (2): An error occurred trying to start process 'pnpm.cmd' with working directory 'C:\project\client-app\'. 지정된 파일을 찾을 수 없습니다.
at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
at Microsoft.AspNetCore.SpaProxy.SpaProxyLaunchManager.LaunchDevelopmentProxy()
fail: Microsoft.AspNetCore.SpaProxy.SpaProxyLaunchManager[0]
Couldn't start the SPA development server with command 'pnpm dev'.
info: Microsoft.AspNetCore.SpaProxy.SpaProxyMiddleware[0]
SPA proxy is not ready. Returning temporary landing page.
會出現這錯誤主要原因是因為使用了 pnpm
官方獨立安裝的方法安裝 pnpm
所產生的,如果使用 npm i -g pnpm
安裝倒是可以正常使用。
目前官方已知此問題但修正應該排到很後面了,但有網友給出了一個 workaround,利用 <SpaProxyLaunchCommand>
去叫起一個 shell 來解決:
<SpaProxyLaunchCommand>pnpm i && pnpm dev</SpaProxyLaunchCommand>
<SpaProxyLaunchCommand Condition="$([MSBuild]::IsOsPlatform('Windows'))">cmd.exe /s /c $(SpaProxyLaunchCommand)</SpaProxyLaunchCommand>
<SpaProxyLaunchCommand Condition="$([MSBuild]::IsOSUnixLike())">sh -c '$(SpaProxyLaunchCommand)'</SpaProxyLaunchCommand>
Ref: