How to Install Node.js and npm on Windows Server 2022
Node.js is a JavaScript runtime built on Chrome’s V8 engine that enables server-side scripting. On Windows Server 2022, Node.js is commonly used for running web APIs, build tools, automation scripts, and full-stack applications. This guide covers installation, configuration, package management with npm, running Node.js applications as Windows services, and managing multiple Node versions.
Downloading and Installing Node.js LTS for Windows
Node.js releases come in two tracks: LTS (Long Term Support) and Current. For Windows Server 2022 production environments, always use the LTS release. LTS versions receive bug fixes and security updates for 30 months, making them the right choice for stable server deployments.
Download the Windows Installer (.msi) package from the official Node.js website at nodejs.org. Choose the 64-bit Windows Installer. The .msi installer handles PATH configuration automatically, which is the recommended approach for most server scenarios.
To install silently from PowerShell (useful for automation):
$NodeVersion = "20.14.0"
$InstallerUrl = "https://nodejs.org/dist/v$NodeVersion/node-v$NodeVersion-x64.msi"
$InstallerPath = "$env:TEMPnode-installer.msi"
Invoke-WebRequest -Uri $InstallerUrl -OutFile $InstallerPath
Start-Process msiexec.exe -ArgumentList "/i `"$InstallerPath`" /quiet /norestart ADDLOCAL=ALL" -Wait -NoNewWindow
Remove-Item $InstallerPath
The ADDLOCAL=ALL flag ensures all optional components including npm and documentation are installed. After installation completes, open a new PowerShell window and verify:
node --version
npm --version
Adding Node.js to PATH Manually
If you installed Node.js via a zip archive or a non-standard location, you must add it to the system PATH manually. This applies to scenarios where you extract Node.js into a directory like C:nodejs.
# Add Node.js to system PATH permanently
$NodePath = "C:nodejs"
$CurrentPath = [Environment]::GetEnvironmentVariable("PATH", "Machine")
if ($CurrentPath -notlike "*$NodePath*") {
[Environment]::SetEnvironmentVariable(
"PATH",
"$CurrentPath;$NodePath",
"Machine"
)
Write-Host "Node.js added to system PATH"
} else {
Write-Host "Node.js already in PATH"
}
Close and reopen any PowerShell or Command Prompt sessions after modifying PATH for changes to take effect. You can verify the path is active in the current session by running $env:PATH -split ";".
npm Basics: Install, Update, and List
npm (Node Package Manager) is installed alongside Node.js and is the primary tool for managing JavaScript dependencies. Understanding the core commands is essential for any Node.js workflow.
Initialize a new Node.js project in the current directory:
npm init -y
Install a package and save it as a dependency:
npm install express
Install a package as a development dependency only:
npm install --save-dev nodemon
Update all packages in the current project to their latest compatible versions:
npm update
List all installed packages in the current project:
npm list
List only top-level packages (not nested dependencies):
npm list --depth=0
npm Global vs Local Packages
npm installs packages in two modes. Local installation places packages in the node_modules folder of the current project. Global installation places packages in a system-wide location accessible from any directory.
Install a package globally:
npm install -g pm2
List all globally installed packages:
npm list -g --depth=0
The global npm modules directory on Windows is typically %APPDATA%npm for the current user or %ProgramFiles%nodejsnode_modulesnpm for system-wide installs. To see the exact global prefix:
npm config get prefix
On Windows Server 2022, when running as a service or under a specific service account, the global prefix may point to that account’s AppData. Explicitly setting the prefix is recommended for server environments:
npm config set prefix "C:npm-global"
# Then add C:npm-global to PATH
Managing Multiple Node.js Versions with nvm-windows
nvm-windows (Node Version Manager for Windows) allows you to install and switch between multiple Node.js versions on the same server. This is valuable when different applications require different Node.js versions.
Download the nvm-setup.exe installer from the nvm-windows GitHub releases page. Run it and follow the wizard. After installation:
# List available Node.js versions
nvm list available
# Install a specific version
nvm install 18.20.3
# Install the latest LTS
nvm install lts
# Switch to a specific version
nvm use 18.20.3
# See all installed versions
nvm list
# Check currently active version
node --version
nvm-windows works by symlinking the selected Node.js version into a common path (default: C:Users[user]AppDataRoamingnvm). Each version’s global npm packages are stored separately, so you must reinstall global tools when switching versions.
Configuring npm Proxy Settings
In corporate Windows Server 2022 environments, outbound HTTP traffic often requires a proxy. npm must be configured to route requests through the proxy server for package downloads to succeed.
# Set HTTP proxy
npm config set proxy http://proxy.corp.local:8080
# Set HTTPS proxy
npm config set https-proxy http://proxy.corp.local:8080
# If the proxy requires authentication
npm config set proxy http://username:[email protected]:8080
# Verify proxy settings
npm config get proxy
npm config get https-proxy
To disable proxy verification for internal certificate authorities (use with caution):
npm config set strict-ssl false
A better approach is to add your corporate CA certificate to the npm trusted CAs:
npm config set cafile "C:certscorp-ca.pem"
npm Cache Management
npm maintains a local cache of downloaded packages to avoid repeated downloads. Over time this cache can grow large, and occasionally a corrupted cache causes mysterious install failures.
# View the cache location
npm config get cache
# Verify the cache integrity
npm cache verify
# Clean the cache (forces re-download on next install)
npm cache clean --force
On Windows Server 2022, the cache is typically stored at %APPDATA%npm-cache. In automated build pipelines, controlling the cache location helps manage disk usage:
npm config set cache "D:npm-cache"
Running Node.js Applications as Windows Services with NSSM
NSSM (Non-Sucking Service Manager) is the most reliable tool for wrapping Node.js applications as Windows services. It handles automatic restart on crash, output logging, and service lifecycle management.
Download nssm.exe from nssm.cc. Place it in a directory on your PATH, such as C:toolsnssm.
# Install a Node.js app as a service
nssm install MyNodeApp
# Or install non-interactively
nssm install MyNodeApp "C:nodejsnode.exe" "C:appsmyappserver.js"
# Set the working directory
nssm set MyNodeApp AppDirectory "C:appsmyapp"
# Configure stdout and stderr logging
nssm set MyNodeApp AppStdout "C:logsmyapp-stdout.log"
nssm set MyNodeApp AppStderr "C:logsmyapp-stderr.log"
# Rotate logs to prevent unbounded growth
nssm set MyNodeApp AppRotateFiles 1
nssm set MyNodeApp AppRotateBytes 10485760
# Start the service
nssm start MyNodeApp
# Check service status
nssm status MyNodeApp
To pass environment variables to the Node.js process running as a service:
nssm set MyNodeApp AppEnvironmentExtra "NODE_ENV=production" "PORT=3000"
Running Node.js as a Service with node-windows
node-windows is an npm package that provides a JavaScript API for creating Windows services from Node.js itself, without requiring external tools.
npm install -g node-windows
npm link node-windows
Create a service installer script install-service.js:
var Service = require('node-windows').Service;
var svc = new Service({
name: 'MyNodeApp',
description: 'My Node.js Application',
script: 'C:\apps\myapp\server.js',
nodeOptions: ['--harmony', '--max_old_space_size=4096'],
env: [
{ name: "NODE_ENV", value: "production" },
{ name: "PORT", value: "3000" }
]
});
svc.on('install', function() {
svc.start();
console.log('Service installed and started.');
});
svc.install();
node install-service.js
Configuring .npmrc
The .npmrc file stores npm configuration persistently. There are three levels: per-project (.npmrc in the project root), per-user (%USERPROFILE%.npmrc), and global (%ProgramFiles%nodejsnode_modulesnpmnpmrc).
A typical server-side .npmrc for a corporate environment:
registry=https://nexus.corp.local/repository/npm-proxy/
proxy=http://proxy.corp.local:8080
https-proxy=http://proxy.corp.local:8080
cafile=C:certscorp-ca.pem
cache=D:npm-cache
save-exact=true
audit=true
fund=false
Set individual values via command line (writes to user-level .npmrc):
npm config set registry https://nexus.corp.local/repository/npm-proxy/
npm config list
npm Audit for Security Vulnerability Scanning
npm audit checks your project’s dependency tree against the npm security advisory database and reports known vulnerabilities.
# Run a security audit
npm audit
# Get a JSON report for automated processing
npm audit --json
# Automatically fix vulnerabilities where possible
npm audit fix
# Fix including semver-major updates (may introduce breaking changes)
npm audit fix --force
# Audit without modifying package-lock.json
npm audit --dry-run
In a CI/CD pipeline on Windows Server 2022, integrate npm audit into your build script to block deployments when high-severity vulnerabilities are found:
# PowerShell CI check - fail build on high/critical vulnerabilities
$AuditResult = npm audit --json | ConvertFrom-Json
$HighVulns = $AuditResult.metadata.vulnerabilities.high
$CriticalVulns = $AuditResult.metadata.vulnerabilities.critical
if ($HighVulns -gt 0 -or $CriticalVulns -gt 0) {
Write-Error "Build blocked: $CriticalVulns critical, $HighVulns high severity vulnerabilities found."
exit 1
}
Updating npm Itself
npm ships with Node.js but is updated more frequently. Update npm independently:
# Update npm to the latest version
npm install -g npm@latest
# Update to a specific version
npm install -g [email protected]
# Verify the update
npm --version
On Windows, updating npm while Node.js is installed via the MSI can sometimes cause version conflicts. If npm becomes unresponsive after an update, reinstall Node.js using the MSI and do not manually update npm separately in that configuration.
Summary
Node.js on Windows Server 2022 is a capable platform for hosting APIs, build tools, and automation services. Install Node.js LTS via the MSI for automatic PATH configuration, use nvm-windows when multiple versions are needed, configure npm for your corporate network via .npmrc or npm config commands, and wrap long-running Node.js processes as Windows services using NSSM for production reliability. Regularly run npm audit as part of your security hygiene to stay ahead of dependency vulnerabilities.