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:
Option 2: Using Node Version Manager (NVM)
NVM allows you to install and switch between multiple Node.js versions.
On macOS/Linux:
On Windows:
Option 3: Using Package Managers
macOS (Homebrew):
Ubuntu/Debian:
Windows (Chocolatey):
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
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:
3. Git Version Control
Project Structure Setup
Creating Your First Project
Basic package.json Structure
Essential .gitignore
Environment Variables
Using dotenv Package
Create .env file:
Load in your application:
Development Workflow Setup
1. ESLint (Code Linting)
2. Prettier (Code Formatting)
Create .prettierrc:
3. Package Scripts
Update package.json scripts:
Debugging Setup
VS Code Debugging Configuration
Create .vscode/launch.json:
Node.js Inspector
Verification Test
Create a simple test file to verify your setup:
Run the test:
Troubleshooting Common Issues
Permission Issues (macOS/Linux)
Path Issues (Windows)
- Ensure Node.js is added to system PATH
- Restart command prompt after installation
Version Conflicts
Next 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