Environment Setup
Overview
Setting up a proper development environment is crucial for Node.js development. This chapter will guide you through installing Node.js, NPM, and essential development tools.
Installing Node.js
Option 1: Official Installer (Recommended for Beginners)
- Visit nodejs.org
- Download the LTS (Long Term Support) version
- Run the installer and follow the setup wizard
- Verify installation:
bash
node --version
npm --versionOption 2: Using Node Version Manager (NVM)
NVM allows you to install and switch between multiple Node.js versions.
On macOS/Linux:
bash
# Install NVM
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash
# Restart terminal or run:
source ~/.bashrc
# Install latest LTS Node.js
nvm install --lts
nvm use --ltsOn Windows:
bash
# Install nvm-windows from GitHub releases
# Then use PowerShell:
nvm install lts
nvm use ltsOption 3: Using Package Managers
macOS (Homebrew):
bash
brew install nodeUbuntu/Debian:
bash
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt-get install -y nodejsWindows (Chocolatey):
bash
choco install nodejsUnderstanding NPM
NPM (Node Package Manager) comes bundled with Node.js and serves multiple purposes:
- Package Manager: Install and manage dependencies
- Script Runner: Run custom scripts defined in package.json
- Registry: Access to over 1 million packages
Basic NPM Commands
bash
# Check NPM version
npm --version
# Initialize a new project
npm init
# Install a package locally
npm install express
# Install a package globally
npm install -g nodemon
# Install development dependencies
npm install --save-dev jest
# Update packages
npm update
# List installed packages
npm listEssential Development Tools
1. Code Editor
Visual Studio Code (Recommended)
- Download from code.visualstudio.com
- Install Node.js extensions:
- Node.js Extension Pack
- ESLint
- Prettier
- REST Client
2. Nodemon (Development Server)
Automatically restarts your application when files change:
bash
# Install globally
npm install -g nodemon
# Use instead of node
nodemon app.js3. Git Version Control
bash
# Install Git
# Windows: Download from git-scm.com
# macOS: brew install git
# Linux: sudo apt install git
# Configure Git
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"Project Structure Setup
Creating Your First Project
bash
# Create project directory
mkdir my-node-app
cd my-node-app
# Initialize NPM project
npm init -y
# Create basic files
touch app.js
touch .gitignoreBasic package.json Structure
json
{
"name": "my-node-app",
"version": "1.0.0",
"description": "My first Node.js application",
"main": "app.js",
"scripts": {
"start": "node app.js",
"dev": "nodemon app.js",
"test": "jest"
},
"keywords": ["nodejs", "javascript"],
"author": "Your Name",
"license": "MIT",
"dependencies": {},
"devDependencies": {}
}Essential .gitignore
gitignore
# Dependencies
node_modules/
npm-debug.log*
# Environment variables
.env
.env.local
.env.production
# Logs
logs/
*.log
# Runtime data
pids/
*.pid
*.seed
# Coverage directory used by tools like istanbul
coverage/
# IDE files
.vscode/
.idea/
*.swp
*.swo
# OS generated files
.DS_Store
Thumbs.dbEnvironment Variables
Using dotenv Package
bash
npm install dotenvCreate .env file:
env
PORT=3000
DATABASE_URL=mongodb://localhost:27017/myapp
API_KEY=your-secret-api-keyLoad in your application:
javascript
require('dotenv').config();
const port = process.env.PORT || 3000;
const dbUrl = process.env.DATABASE_URL;Development Workflow Setup
1. ESLint (Code Linting)
bash
npm install --save-dev eslint
npx eslint --init2. Prettier (Code Formatting)
bash
npm install --save-dev prettierCreate .prettierrc:
json
{
"semi": true,
"trailingComma": "es5",
"singleQuote": true,
"printWidth": 80,
"tabWidth": 2
}3. Package Scripts
Update package.json scripts:
json
{
"scripts": {
"start": "node app.js",
"dev": "nodemon app.js",
"test": "jest",
"lint": "eslint .",
"format": "prettier --write ."
}
}Debugging Setup
VS Code Debugging Configuration
Create .vscode/launch.json:
json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/app.js",
"env": {
"NODE_ENV": "development"
}
}
]
}Node.js Inspector
bash
# Start with debugger
node --inspect app.js
# Or with nodemon
nodemon --inspect app.jsVerification Test
Create a simple test file to verify your setup:
javascript
// app.js
console.log('Node.js version:', process.version);
console.log('NPM version:', process.env.npm_version);
console.log('Environment setup complete!');
// Test async operation
setTimeout(() => {
console.log('Async operation working!');
}, 1000);Run the test:
bash
node app.jsTroubleshooting Common Issues
Permission Issues (macOS/Linux)
bash
# Fix NPM permissions
sudo chown -R $(whoami) ~/.npmPath Issues (Windows)
- Ensure Node.js is added to system PATH
- Restart command prompt after installation
Version Conflicts
bash
# Clear NPM cache
npm cache clean --force
# Reinstall Node.js if neededNext Steps
Now that your environment is set up, we'll create your first Node.js application in the next chapter.
Practice Exercise
- Install Node.js and verify the installation
- Create a new project with
npm init - Install nodemon globally
- Create a simple "Hello World" application
- Set up ESLint and Prettier
Key Takeaways
- Multiple ways to install Node.js (official installer, NVM, package managers)
- NPM is essential for package management and script running
- Proper project structure and tooling improve development experience
- Environment variables help manage configuration
- Development tools like nodemon, ESLint, and Prettier enhance productivity