How to Set Up Node.js: Complete Guide
Node.js lets you run JavaScript on your computer, not just in browsers. It's essential for web development, and it's pretty easy to set up. I've installed Node.js dozens of times, and I'm going to show you the easiest way.
By the end of this guide, you'll have Node.js installed, verified, and ready to use.
What is Node.js?
Node.js is a JavaScript runtime - it lets you run JavaScript code on your computer. You need it for:
- Building web applications
- Using npm (Node Package Manager) to install packages
- Running JavaScript tools and frameworks
- Backend development
Step 1: Download Node.js
Windows and Mac:
- Go to nodejs.org
- Download the LTS (Long Term Support) version - it's the stable one
- Run the installer
- Use the default options - they're fine
The installer includes Node.js and npm (Node Package Manager). Both get installed together.
Mac alternative: If you use Homebrew, you can install with brew install node
Step 2: Verify Installation
Open Command Prompt (Windows) or Terminal (Mac) and check:
node --version
You should see something like "v20.10.0" (version might be different). If you see this, Node.js is installed!
Also check npm:
npm --version
You should see npm's version. npm comes with Node.js, so if Node.js works, npm should too.
Step 3: Test Node.js
Let's make sure it actually works:
node
You should see the Node.js prompt (>). Type:
console.log("Hello, Node.js!")
Press Enter. You should see "Hello, Node.js!" printed. Type .exit or press Ctrl+C twice to leave.
Step 4: Create Your First Node.js File
Create a file called hello.js and type:
console.log("Hello from Node.js!")
Save it, then run:
node hello.js
You should see "Hello from Node.js!" printed. Congratulations - you just ran a Node.js program!
Using npm
npm lets you install packages (libraries) for your projects:
Install a package:
npm install package-name
Initialize a project:
npm init
This creates a package.json file that tracks your project's dependencies.
Install and save to package.json:
npm install package-name --save
The --save flag adds it to your package.json (it's the default now, so you can omit it).
Common Issues and Fixes
- "node is not recognized": Node.js isn't in your PATH. Restart your terminal/command prompt. If that doesn't work, reinstall Node.js and make sure to check "Add to PATH" during installation.
- Permission errors on Mac/Linux: Don't use
sudowith npm install. If you get permission errors, fix npm's permissions or use a node version manager. - Old version: Make sure you downloaded the latest LTS version from nodejs.org.
- npm not found: npm comes with Node.js. If npm doesn't work, Node.js might not be installed correctly. Reinstall Node.js.
Using nvm (Node Version Manager) - Optional
nvm lets you install and switch between multiple Node.js versions. Useful if you work on different projects:
Install nvm:
- Mac/Linux: Go to github.com/nvm-sh/nvm for installation instructions
- Windows: Use nvm-windows from github.com/coreybutler/nvm-windows
Use nvm:
nvm install node # Install latest
nvm install 20.10.0 # Install specific version
nvm use 20.10.0 # Switch to version
nvm list # List installed versions
nvm is useful but not necessary for beginners. You can install it later if you need multiple versions.
Next Steps
Now that Node.js is installed:
- Start learning JavaScript/Node.js
- Create your first Node.js project
- Install useful packages with npm
- Build web applications
Check out our web development courses to get started!
Pro Tip: Keep Node.js updated. New versions come out regularly with improvements and security fixes. Check nodejs.org for updates, or use nvm to easily switch versions. The LTS (Long Term Support) version is the most stable - use that unless you need specific newer features.
Common Questions
Do I need to know JavaScript to use Node.js?
Yes, Node.js runs JavaScript. If you don't know JavaScript, learn that first. Node.js is just the runtime that executes JavaScript code on your computer.
What's the difference between Node.js and npm?
Node.js is the JavaScript runtime (runs JavaScript code). npm is the package manager (installs libraries/packages). They come together - when you install Node.js, you get npm too.
Should I use the LTS or Current version?
Use LTS (Long Term Support) for most projects. It's more stable and supported longer. Current version has newer features but might have bugs. For learning and most projects, LTS is better.
Start Using Node.js
Node.js is installed and working. Now start building! Create a simple JavaScript file, run it with Node.js, install some packages with npm. The more you use it, the more comfortable you'll get. Node.js opens up a lot of possibilities for web development.