โ† JS Mastery | Module 1: Getting Started Setting Up Node.js
Module 1 ยท Lesson 3

Setting Up Node.js & Your Dev Environment

โฑ 14 min read โ— Beginner ๐Ÿ†“ Free

Why Node.js?

Node.js lets you run JavaScript outside the browser. It's built on Chrome's V8 engine and gives you access to the file system, network, and system resources. But for this course, the main reason you need it is simpler: it lets you run JavaScript files from the command line, which is far more practical than typing everything in the browser console.

When you install Node.js, you also get npm (Node Package Manager) โ€” the world's largest software registry with over 2 million packages. You'll use npm to install libraries that make your life easier.

Installing Node.js

Option 1: Direct Download (Simplest)

Go to nodejs.org and download the LTS (Long Term Support) version. LTS is the stable version โ€” use it unless you specifically need the latest features.

Option 2: Node Version Manager (Recommended)

For serious development, use nvm (Mac/Linux) or nvm-windows. It lets you install multiple Node versions and switch between them:

// On Mac/Linux, install nvm:
// curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

// Then install and use the latest LTS:
// nvm install --lts
// nvm use --lts

// Check what's installed:
// nvm list

Verify Installation

Open your terminal and run these commands:

// In your terminal (not browser console):
// node --version      โ†’ should print v20.x.x or similar
// npm --version       โ†’ should print 10.x.x or similar

// Start the Node.js REPL (interactive shell):
// node
// > 1 + 1
// 2
// > console.log("Hello Node!")
// Hello Node!

Setting Up VS Code

Visual Studio Code is the most popular code editor for JavaScript development. It's free, open-source, and has outstanding JavaScript support built in.

Download from code.visualstudio.com. After installing, add these extensions:

ExtensionWhat it doesPriority
ESLintCatches errors and style issues as you typeEssential
PrettierAuto-formats your code on saveEssential
JavaScript (ES6) code snippetsShortcuts for common patternsHigh
GitLensSupercharged git integrationMedium
Error LensShows errors inline in your codeHigh

VS Code Settings for JavaScript

Open Settings (Ctrl+,) and configure these:

// settings.json (Ctrl+Shift+P โ†’ "Open Settings JSON")
{
  "editor.formatOnSave": true,
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "editor.tabSize": 2,
  "javascript.updateImportsOnFileMove.enabled": "always",
  "editor.quickSuggestions": {
    "strings": true
  }
}

Your First Node.js Project

Let's create a proper project structure from scratch:

// In your terminal:
// mkdir my-first-js-project
// cd my-first-js-project
// npm init -y          โ† creates package.json with defaults

// Create a file called index.js and add:
const greeting = "Hello from Node.js!";
console.log(greeting);

const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(n => n * 2);
console.log("Doubled:", doubled);

const sum = numbers.reduce((acc, n) => acc + n, 0);
console.log("Sum:", sum);

// Run it:
// node index.js

Understanding package.json

The package.json file is the heart of any Node.js project. It describes your project and its dependencies:

{
  "name": "my-first-js-project",
  "version": "1.0.0",
  "description": "Learning JavaScript with Nixus",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "dev": "node --watch index.js"   // auto-restart on file change
  },
  "keywords": ["javascript", "learning"],
  "author": "Your Name",
  "license": "MIT"
}

// Now you can run: npm start
// Or: npm run dev (auto-restarts when you save)

Installing and Using Packages

One of npm's greatest features is access to millions of libraries. Let's install one:

// Install the popular 'chalk' package for colorful terminal output
// npm install chalk

// chalk v5+ is ESM only, so use v4 for CommonJS:
// npm install chalk@4

// Now use it in index.js:
const chalk = require('chalk');

console.log(chalk.yellow('Hello in yellow!'));
console.log(chalk.green.bold('Success!'));
console.log(chalk.red('Error occurred'));
console.log(chalk.blue.bgWhite('Blue text, white background'));

// node index.js โ†’ colorful output!

When you install a package, npm creates a node_modules/ folder and a package-lock.json file. Never commit node_modules to git โ€” add it to .gitignore.

// .gitignore file (create in project root)
node_modules/
.env
*.log
dist/
.DS_Store

Running JavaScript Files

There are several ways to run JavaScript outside the browser:

// Method 1: Direct execution
// node script.js

// Method 2: npm script
// "start": "node script.js"
// npm start

// Method 3: With nodemon (auto-restart on changes)
// npm install -g nodemon
// nodemon script.js

// Method 4: Node REPL (interactive)
// node
// > const x = 5
// > x * 2
// 10

// Method 5: Inline code (rare)
// node -e "console.log('hello')"
๐Ÿ’ก Modern Setup

Node.js 18+ has a built-in --watch flag: node --watch index.js. This automatically restarts your script when the file changes, replacing the need for nodemon in many cases.

Project Structure Best Practices

// Recommended project structure:
my-project/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ index.js        // entry point
โ”‚   โ”œโ”€โ”€ utils/
โ”‚   โ”‚   โ””โ”€โ”€ helpers.js  // utility functions
โ”‚   โ””โ”€โ”€ modules/
โ”‚       โ””โ”€โ”€ math.js     // specific functionality
โ”œโ”€โ”€ tests/
โ”‚   โ””โ”€โ”€ math.test.js    // tests go here
โ”œโ”€โ”€ .gitignore
โ”œโ”€โ”€ .env                // environment variables (never commit!)
โ”œโ”€โ”€ package.json
โ”œโ”€โ”€ package-lock.json
โ””โ”€โ”€ README.md           // document your project!

โšก Key Takeaways

๐ŸŽฏ Practice Exercises

EXERCISE 1

Create a new project folder, run npm init -y, create an index.js that prints the current date and time using new Date().toString(). Run it with node index.js.

EXERCISE 2

Add a start script to package.json that runs your file. Run it with npm start. What's the difference from running node index.js directly?

EXERCISE 3 โ€” CHALLENGE

Install the lodash package (npm install lodash). Use _.chunk([1,2,3,4,5,6], 2) to split an array into pairs. Check the lodash docs for 2 other useful functions and try them out.

โ† Browser Console