Skip to content

Commit 903fffa

Browse files
committed
[edit] npm installation made cross platform
1 parent 2890ecb commit 903fffa

2 files changed

Lines changed: 38 additions & 4 deletions

File tree

src/SampleApps/SampleApp.Angular/SampleApp.Angular.csproj

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,34 @@
2929
<None Include="$(SpaRoot)**" Exclude="$(SpaRoot)node_modules\**" />
3030
</ItemGroup>
3131

32+
<!--
33+
Cross-platform node/npm invocation.
34+
On Windows, MSBuild inherits PATH from the OS (where the Node installer
35+
usually places node), so we call the executables directly.
36+
On Unix (Linux/macOS), version managers like nvm / fnm / volta only inject
37+
node into PATH inside interactive shells (.zshrc / .bashrc). MSBuild spawns
38+
a non-interactive process, so we route the command through the user's
39+
default $SHELL with -i -l -c to source those rc files.
40+
-->
41+
<PropertyGroup>
42+
<_NodeShellPrefix Condition=" '$(OS)' != 'Windows_NT' ">"$SHELL" -ilc </_NodeShellPrefix>
43+
<_NodeVersionCommand Condition=" '$(OS)' == 'Windows_NT' ">node --version</_NodeVersionCommand>
44+
<_NodeVersionCommand Condition=" '$(OS)' != 'Windows_NT' ">$(_NodeShellPrefix)"node --version"</_NodeVersionCommand>
45+
<_NpmInstallCommand Condition=" '$(OS)' == 'Windows_NT' ">npm install</_NpmInstallCommand>
46+
<_NpmInstallCommand Condition=" '$(OS)' != 'Windows_NT' ">$(_NodeShellPrefix)"npm install"</_NpmInstallCommand>
47+
</PropertyGroup>
48+
3249
<Target Name="DebugEnsureNodeEnv" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('$(SpaRoot)node_modules') ">
3350
<Message Importance="high" Text="Current node version:" />
3451

35-
<Exec Command="node --version" ContinueOnError="true">
52+
<Exec Command="$(_NodeVersionCommand)" ContinueOnError="true">
3653
<Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
3754
</Exec>
3855

3956
<Error Condition="'$(ErrorCode)' != '0'" Text="Node.js is required to build and run this project. To continue, please install Node.js from https://nodejs.org/, and then restart your command prompt or IDE." />
4057

4158
<Message Importance="high" Text="Restoring dependencies using 'npm'. This may take several minutes..." />
4259

43-
<Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
60+
<Exec WorkingDirectory="$(SpaRoot)" Command="$(_NpmInstallCommand)" />
4461
</Target>
4562
</Project>

src/SampleApps/SampleApp.Classic/SampleApp.Classic.csproj

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,34 @@
3838
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
3939
</None>
4040
</ItemGroup>
41+
<!--
42+
Cross-platform node/npm invocation.
43+
On Windows, MSBuild inherits PATH from the OS (where the Node installer
44+
usually places node), so we call the executables directly.
45+
On Unix (Linux/macOS), version managers like nvm / fnm / volta only inject
46+
node into PATH inside interactive shells (.zshrc / .bashrc). MSBuild spawns
47+
a non-interactive process, so we route the command through the user's
48+
default $SHELL with -i -l -c to source those rc files.
49+
-->
50+
<PropertyGroup>
51+
<_NodeShellPrefix Condition=" '$(OS)' != 'Windows_NT' ">"$SHELL" -ilc </_NodeShellPrefix>
52+
<_NodeVersionCommand Condition=" '$(OS)' == 'Windows_NT' ">node --version</_NodeVersionCommand>
53+
<_NodeVersionCommand Condition=" '$(OS)' != 'Windows_NT' ">$(_NodeShellPrefix)"node --version"</_NodeVersionCommand>
54+
<_NpmInstallCommand Condition=" '$(OS)' == 'Windows_NT' ">npm install</_NpmInstallCommand>
55+
<_NpmInstallCommand Condition=" '$(OS)' != 'Windows_NT' ">$(_NodeShellPrefix)"npm install"</_NpmInstallCommand>
56+
</PropertyGroup>
57+
4158
<Target Name="RestoreNodePackages" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('node_modules') ">
4259
<Message Importance="high" Text="Current node version:" />
4360

44-
<Exec Command="node --version" ContinueOnError="true">
61+
<Exec Command="$(_NodeVersionCommand)" ContinueOnError="true">
4562
<Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
4663
</Exec>
4764

4865
<Error Condition="'$(ErrorCode)' != '0'" Text="Node.js is required to build and run this project. To continue, please install Node.js from https://nodejs.org/, and then restart your command prompt or IDE." />
4966

5067
<Message Importance="high" Text="Restoring dependencies using 'npm'. This may take several minutes..." />
5168

52-
<Exec Command="npm install" />
69+
<Exec Command="$(_NpmInstallCommand)" />
5370
</Target>
5471
</Project>

0 commit comments

Comments
 (0)