I have a .esproj which is used to restore npm packages in Visual Studio.
However it will only restore when there is no package-lock.json
. However since node_modules is in .gitignore, developers who checkout the repo will have a package-lock.json
, but no modules downloaded yet.
So how can I force Visual Studio to restore npm packages when package-lock.json
is present?
Using:
Microsoft Visual Studio Professional 2022
Version 17.10.4
I used a hash file to detect when it changed:
<code><Project Sdk="Microsoft.VisualStudio.JavaScript.Sdk/0.5.89-alpha">
<PropertyGroup>
<ShouldRunNpmInstall>false</ShouldRunNpmInstall>
<ShouldRunNpmBuildScript>false</ShouldRunNpmBuildScript>
<!-- Define the paths for package-lock.json and the hash file -->
<PackageLockFile>$(ProjectDir)package-lock.json</PackageLockFile>
<PackageLockHashFile>$(ProjectDir)package-lock.hash</PackageLockHashFile>
</PropertyGroup>
<Target Name="CheckNpmPackages" BeforeTargets="Build">
<!-- Calculate the file hash using GetFileHash -->
<GetFileHash Files="$(PackageLockFile)" Algorithm="SHA256">
<Output TaskParameter="Hash" PropertyName="CurrentHash" />
</GetFileHash>
<!-- Read previous hash from file, only if it exists -->
<ReadLinesFromFile File="$(PackageLockHashFile)" Condition="Exists('$(PackageLockHashFile)')">
<Output TaskParameter="Lines" PropertyName="PreviousHash" />
</ReadLinesFromFile>
<!-- Run npm ci if the hash has changed -->
<Exec Command="npm ci --no-audit" Condition="$(CurrentHash) != $(PreviousHash)" />
<!-- Save the current hash to the file -->
<WriteLinesToFile File="$(PackageLockHashFile)" Lines="$(CurrentHash)" Overwrite="true" />
</Target>
</Project>
</code>
<code><Project Sdk="Microsoft.VisualStudio.JavaScript.Sdk/0.5.89-alpha">
<PropertyGroup>
<ShouldRunNpmInstall>false</ShouldRunNpmInstall>
<ShouldRunNpmBuildScript>false</ShouldRunNpmBuildScript>
<!-- Define the paths for package-lock.json and the hash file -->
<PackageLockFile>$(ProjectDir)package-lock.json</PackageLockFile>
<PackageLockHashFile>$(ProjectDir)package-lock.hash</PackageLockHashFile>
</PropertyGroup>
<Target Name="CheckNpmPackages" BeforeTargets="Build">
<!-- Calculate the file hash using GetFileHash -->
<GetFileHash Files="$(PackageLockFile)" Algorithm="SHA256">
<Output TaskParameter="Hash" PropertyName="CurrentHash" />
</GetFileHash>
<!-- Read previous hash from file, only if it exists -->
<ReadLinesFromFile File="$(PackageLockHashFile)" Condition="Exists('$(PackageLockHashFile)')">
<Output TaskParameter="Lines" PropertyName="PreviousHash" />
</ReadLinesFromFile>
<!-- Run npm ci if the hash has changed -->
<Exec Command="npm ci --no-audit" Condition="$(CurrentHash) != $(PreviousHash)" />
<!-- Save the current hash to the file -->
<WriteLinesToFile File="$(PackageLockHashFile)" Lines="$(CurrentHash)" Overwrite="true" />
</Target>
</Project>
</code>
<Project Sdk="Microsoft.VisualStudio.JavaScript.Sdk/0.5.89-alpha">
<PropertyGroup>
<ShouldRunNpmInstall>false</ShouldRunNpmInstall>
<ShouldRunNpmBuildScript>false</ShouldRunNpmBuildScript>
<!-- Define the paths for package-lock.json and the hash file -->
<PackageLockFile>$(ProjectDir)package-lock.json</PackageLockFile>
<PackageLockHashFile>$(ProjectDir)package-lock.hash</PackageLockHashFile>
</PropertyGroup>
<Target Name="CheckNpmPackages" BeforeTargets="Build">
<!-- Calculate the file hash using GetFileHash -->
<GetFileHash Files="$(PackageLockFile)" Algorithm="SHA256">
<Output TaskParameter="Hash" PropertyName="CurrentHash" />
</GetFileHash>
<!-- Read previous hash from file, only if it exists -->
<ReadLinesFromFile File="$(PackageLockHashFile)" Condition="Exists('$(PackageLockHashFile)')">
<Output TaskParameter="Lines" PropertyName="PreviousHash" />
</ReadLinesFromFile>
<!-- Run npm ci if the hash has changed -->
<Exec Command="npm ci --no-audit" Condition="$(CurrentHash) != $(PreviousHash)" />
<!-- Save the current hash to the file -->
<WriteLinesToFile File="$(PackageLockHashFile)" Lines="$(CurrentHash)" Overwrite="true" />
</Target>
</Project>