Before diving into Node.js development, you’ll need to set up your environment properly. This guide covers installation, essential tools, and verification steps.
1. Installing Node.js
Windows/Mac
- Visit nodejs.org
- Download the LTS version (recommended for most users)
- Run the installer with default settings
Linux (Ubuntu/Debian)
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt-get install -y nodejs
Pro Tip: Avoid installing Node.js from your Linux distro’s default package manager as it often provides outdated versions.
2. Verify Your Installation
Open your terminal/command prompt and run:
node -v
npm -v
You should see version numbers like:
v18.16.0 # Node.js version
9.5.1 # npm version
3. Essential Tools
Tool | Purpose | Installation Command |
---|---|---|
nvm | Node Version Manager (switch between versions) | curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash |
nodemon | Auto-restart server during development | npm install -g nodemon |
VS Code | Recommended code editor | Download from official site |
4. Configuring Your First Project
Create a new project directory and initialize it:
mkdir my-first-node-app
cd my-first-node-app
npm init -y
This generates a package.json
file with default settings.
5. Your First Node.js Script
Create a file named app.js
with this content:
console.log("Hello from Node.js!");
function greet(name) {
console.log(`Welcome ${name}!`);
}
greet("Node Developer");
// Run this with: node app.js
6. Running and Debugging
Basic Execution
node app.js
Outputs:
Hello from Node.js!
Welcome Node Developer!
Debug Mode
node inspect app.js
Use Chrome DevTools by visiting:chrome://inspect
Common Setup Issues
Permission Errors on Linux/Mac
Fix by either:
- Using nvm (recommended)
- Reinstalling Node.js with correct permissions
- Changing npm’s default directory:
mkdir ~/.npm-global npm config set prefix '~/.npm-global'
Version Conflicts
Use nvm to manage multiple versions:
nvm install 18.16.0 # Install specific version
nvm use 18.16.0 # Switch to version
Next: Understanding Node Modules & npm →
Cheat Sheet
node --version
– Check Node.js versionnpm install -g package
– Install global packagenpm init
– Create new projectnode file.js
– Run JavaScript file