TeamCity and avoiding redownloading of npm packages

I haven’t been able to find a better solution that this. My original post on stackoverflow here.

TeamCity clears out the node_modules folder on every build, so when running npm install it re-downloads them all. So I’ve moved to using a powershell step to backup and restore this folder and the end and start of each build respectively.

Here is the PS step i run at the start


param (
[string]$projId ,
[string]$WorkDir
)

if(Test-Path "c:\node_backup\$projId\node_modules")
{
Move-Item "c:\node_backup\$projId\node_modules" "$WorkDir\node_modules"
}

And here is the one at the end


param (
[string]$projId ,
[string]$WorkDir
)
If (!(Test-Path "c:\node_backup"))
{
mkdir "c:\node_backup"
}
If (!(Test-Path c:\node_backup\$projId))
{
mkdir "c:\node_backup\$projId"
}
If (Test-Path "c:\node_backup\$projId\node_modules")
{
Remove-Item "c:\node_backup\$projId\node_modules"
}

Move-Item "$WorkDir\node_modules" "c:\node_backup\$projId"

Then in the script argumentsĀ i pass this

-projId %system.teamcity.buildType.id% -WorkDir %NodeWorkingDirectory%

The NodeWorkingDir is a parameter I set in my projects that i use to tell node where my gulp file is.