Node.js Setup Guide - CodeSwordsman

Set up Node.js from scratch: install on Windows and Linux/macOS, switch versions with nvm or fnm, switch registries, enable pnpm, scaffold projects and fix common errors.

Node.js 20 LTS / 22 LTSUpdated 2026-10-2929 commands

Install & Version Management

Install Node.js LTS on Windows; reopen the terminal afterwards so PATH takes effect

# Windows(官方 LTS 安装包走 winget)
winget install OpenJS.NodeJS.LTS

Use Homebrew on macOS and the NodeSource repo on Ubuntu to install a specific major version

# macOS
brew install node@20
# Ubuntu / Debian
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs

Install fnm on Windows as the version manager (faster than nvm-windows, no admin rights needed)

winget install Schniz.fnm

Install and switch to Node 20, then list locally installed versions

fnm install 20
fnm use 20
fnm list

Enable fnm env --use-on-cd in your PowerShell profile to switch versions automatically when entering a directory

Install and switch versions with nvm, and set the default version

# macOS / Linux
nvm install 20
nvm use 20
nvm alias default 20

Enable pnpm; prefer corepack so the whole team uses the same pnpm version

corepack enable pnpm
npm install -g pnpm@9

Mirrors & Proxy

Switch npm to a China mirror and verify the active registry

npm config set registry https://registry.npmmirror.com
npm config get registry

pnpm keeps its own config, so set the mirror separately

pnpm config set registry https://registry.npmmirror.com

Write this into the project .npmrc so the mirror only applies to this project

registry=https://registry.npmmirror.com
strict-ssl=true

Configure npm to go through a corporate or local proxy

npm config set proxy http://127.0.0.1:7890
npm config set https-proxy http://127.0.0.1:7890

Remove proxy settings after turning the proxy off, otherwise installs keep timing out

npm config delete proxy
npm config delete https-proxy

Environment Variables

Check the Node/npm versions and the global install prefix

node -v
npm -v
npm config get prefix

Show the global node_modules path when debugging global packages

npm root -g

Set NODE_ENV with setx on Windows or by appending to the shell profile on macOS/Linux

# Windows PowerShell(永久生效,需重开终端)
setx NODE_ENV development
# macOS / Linux
echo 'export NODE_ENV=development' >> ~/.zshrc

Verify the npm global bin directory is on PATH, otherwise global commands are not found

# Windows
echo %APPDATA%\npm
# macOS / Linux
echo $PATH | tr ":" "\n" | grep npm

Create & Verify

Scaffold a Vue + TypeScript project with Vite

npm create vite@latest my-app -- --template vue-ts

Initialize a minimal project and run the entry file directly

npm init -y
node index.js

Install dependencies and start the local dev server

npm install
npm run dev

Build for production and verify the output with preview

npm run build
npm run preview

Print the active Node version and platform to confirm the effective runtime

node -e "console.log(process.version, process.platform)"

IDE & Plugins

Install the Vue, ESLint and Prettier extensions from the CLI so teams can script it

code --install-extension Vue.volar
code --install-extension dbaeumer.vscode-eslint
code --install-extension esbenp.prettier-vscode

Pin the Node version in the repo root so nvm use picks it up automatically

echo 20 > .nvmrc

Declare the engines field so incompatible Node versions are flagged on install

npm pkg set engines.node=">=20"

Troubleshooting

When the version looks wrong, check which executable the command actually resolves to

# Windows
where node
# macOS / Linux
which -a node
Risky

Clean the npm cache when installs fail integrity checks

npm cache verify
npm cache clean --force

The next install re-downloads everything, which is noticeably slower on bad networks

Allow local scripts when PowerShell refuses to load npm.ps1

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

Temporarily disable certificate checks when the corporate MITM certificate causes UNABLE_TO_VERIFY_LEAF_SIGNATURE

npm config set strict-ssl false

Only for intranet setups; set it back to true afterwards to avoid MITM risk

Find the process occupying the dev server port before deciding to kill it

# Windows
netstat -ano | findstr :5173
# macOS / Linux
lsof -i :5173

Fix EACCES on global installs by switching the global prefix to a user directory on Linux/macOS

npm config set prefix ~/.npm-global
echo 'export PATH="$HOME/.npm-global/bin:$PATH"' >> ~/.bashrc

This website uses cookies to ensure you get the best experience on our website. Privacy Policy.