File size: 5,172 Bytes
5a81b95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#Requires -RunAsAdministrator
<#
.SYNOPSIS
    Fixes Node.js installation issues caused by Windows TEMP permission problems.
.DESCRIPTION
    This script:
    1. Fixes C:\Windows\TEMP permissions (root cause of MSI Error 1157)
    2. Uninstalls existing Node.js installation
    3. Cleans up leftover files
    4. Reinstalls Node.js LTS via Chocolatey
.NOTES
    Run this script as Administrator!
    Close VS Code and other applications using Node before running.
#>

$ErrorActionPreference = "Continue"

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "  Node.js Installation Fix Script" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# Check if running as admin
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $isAdmin) {
    Write-Host "ERROR: This script must be run as Administrator!" -ForegroundColor Red
    Write-Host "Right-click PowerShell and select 'Run as Administrator'" -ForegroundColor Yellow
    exit 1
}

# Step 1: Fix Windows\TEMP permissions
Write-Host "[1/7] Fixing C:\Windows\TEMP permissions..." -ForegroundColor Yellow
icacls "C:\Windows\TEMP" /grant "SYSTEM:(OI)(CI)F" /T 2>$null
icacls "C:\Windows\TEMP" /grant "Administrators:(OI)(CI)F" /T 2>$null
icacls "C:\Windows\TEMP" /grant "Users:(OI)(CI)(M)" /T 2>$null
Write-Host "      Done." -ForegroundColor Green

# Step 2: Stop Node processes
Write-Host "[2/7] Stopping Node processes..." -ForegroundColor Yellow
$nodeProcs = Get-Process -Name "node" -ErrorAction SilentlyContinue
if ($nodeProcs) {
    Stop-Process -Name "node" -Force -ErrorAction SilentlyContinue
    Write-Host "      Stopped $($nodeProcs.Count) process(es)." -ForegroundColor Green
} else {
    Write-Host "      No Node processes running." -ForegroundColor Green
}

# Step 3: Reset Windows Installer service
Write-Host "[3/7] Resetting Windows Installer service..." -ForegroundColor Yellow
net stop msiserver 2>$null
Start-Sleep -Seconds 2
net start msiserver 2>$null
Write-Host "      Done." -ForegroundColor Green

# Step 4: Uninstall existing Node via MSI
Write-Host "[4/7] Uninstalling existing Node.js via MSI..." -ForegroundColor Yellow
$nodeProduct = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*" -ErrorAction SilentlyContinue |
    Where-Object { $_.DisplayName -eq "Node.js" }

if ($nodeProduct) {
    $productCode = $nodeProduct.PSChildName
    Write-Host "      Found Node.js with product code: $productCode" -ForegroundColor Gray
    $process = Start-Process -FilePath "msiexec.exe" -ArgumentList "/x $productCode /qn" -Wait -PassThru
    if ($process.ExitCode -eq 0 -or $process.ExitCode -eq 1605) {
        Write-Host "      Uninstalled successfully." -ForegroundColor Green
    } else {
        Write-Host "      MSI uninstall returned code: $($process.ExitCode)" -ForegroundColor Yellow
    }
    Start-Sleep -Seconds 5
} else {
    Write-Host "      No MSI installation found." -ForegroundColor Gray
}

# Step 5: Clean up files
Write-Host "[5/7] Cleaning up leftover files..." -ForegroundColor Yellow
$paths = @(
    "C:\Program Files\nodejs",
    "$env:APPDATA\npm",
    "$env:APPDATA\npm-cache",
    "$env:LOCALAPPDATA\npm-cache"
)
foreach ($path in $paths) {
    if (Test-Path $path) {
        Remove-Item $path -Recurse -Force -ErrorAction SilentlyContinue
        Write-Host "      Removed: $path" -ForegroundColor Gray
    }
}
Write-Host "      Done." -ForegroundColor Green

# Step 6: Clean Chocolatey packages
Write-Host "[6/7] Cleaning Chocolatey packages..." -ForegroundColor Yellow
choco uninstall nodejs nodejs-lts nodejs.install -y --force 2>$null | Out-Null
Remove-Item "C:\ProgramData\chocolatey\lib\nodejs*" -Recurse -Force -ErrorAction SilentlyContinue
Write-Host "      Done." -ForegroundColor Green

# Step 7: Install Node.js LTS
Write-Host "[7/7] Installing Node.js LTS via Chocolatey..." -ForegroundColor Yellow
choco install nodejs-lts -y

# Verify installation
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "  Verification" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan

# Refresh PATH
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")

$nodeVersion = & "C:\Program Files\nodejs\node.exe" --version 2>$null
$npmVersion = & "C:\Program Files\nodejs\npm.cmd" --version 2>$null

if ($nodeVersion) {
    Write-Host "Node.js: $nodeVersion" -ForegroundColor Green
    Write-Host "npm:     $npmVersion" -ForegroundColor Green
    Write-Host ""
    Write-Host "SUCCESS! Node.js is now installed." -ForegroundColor Green
} else {
    Write-Host "WARNING: Could not verify Node.js installation." -ForegroundColor Yellow
    Write-Host "Please restart your terminal and run 'node --version'" -ForegroundColor Yellow
}

Write-Host ""
Write-Host "NOTE: You may need to restart your terminal for PATH changes." -ForegroundColor Cyan