Skip to content

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

  1. Visit nodejs.org
  2. Download the LTS (Long Term Support) version
  3. Run the installer and follow the setup wizard
  4. Verify installation:
bash
node --version
npm --version

Option 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 --lts

On Windows:

bash
# Install nvm-windows from GitHub releases
# Then use PowerShell:
nvm install lts
nvm use lts

Option 3: Using Package Managers

macOS (Homebrew):

bash
brew install node

Ubuntu/Debian:

bash
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt-get install -y nodejs

Windows (Chocolatey):

bash
choco install nodejs

Understanding 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 list

Essential 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.js

3. 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 .gitignore

Basic 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.db

Environment Variables

Using dotenv Package

bash
npm install dotenv

Create .env file:

env
PORT=3000
DATABASE_URL=mongodb://localhost:27017/myapp
API_KEY=your-secret-api-key

Load 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 --init

2. Prettier (Code Formatting)

bash
npm install --save-dev prettier

Create .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.js

Verification 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.js

Troubleshooting Common Issues

Permission Issues (macOS/Linux)

bash
# Fix NPM permissions
sudo chown -R $(whoami) ~/.npm

Path 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 needed

Next Steps

Now that your environment is set up, we'll create your first Node.js application in the next chapter.

Practice Exercise

  1. Install Node.js and verify the installation
  2. Create a new project with npm init
  3. Install nodemon globally
  4. Create a simple "Hello World" application
  5. 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

Content is for learning and research only.