Setting Up NodeJS
Getting started with NodeJS is straightforward. This tutorial will walk you through installation, verification, and running your first Node script.
Installing NodeJS
Option 1: Download from Official Website (Easiest)
- Visit nodejs.org
- Download the LTS version (Long Term Support)
- More stable for production use
- Gets security updates for longer
- Run the installer and follow the prompts
- Restart your terminal/command prompt
Option 2: Using a Version Manager (Recommended for Developers)
On Mac/Linux (using nvm):
# Install nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
# Install latest LTS
nvm install --lts
# Use it
nvm use --lts
On Windows (using nvm-windows):
# Download from github.com/coreybutler/nvm-windows
# Then install specific version:
nvm install 20.10.0
nvm use 20.10.0
Why use a version manager?
- Easily switch between Node versions
- Test your code on different versions
- Avoid permission issues
Verify Installation
After installing, verify it worked:
# Check Node version
node --version
# Should output something like: v20.10.0
# Check npm version (comes with Node)
npm --version
# Should output something like: 10.2.3
Your First Node Script
1. Create a JavaScript file
Create a file called hello.js:
// hello.js
console.log("Hello from NodeJS!");
console.log("Node version:", process.version);
console.log("Platform:", process.platform);
2. Run it with Node
node hello.js
Output:
Hello from NodeJS!
Node version: v20.10.0
Platform: darwin
The Node REPL
REPL stands for Read-Eval-Print-Loop. It’s an interactive JavaScript environment:
# Start REPL
node
# Now you can type JavaScript:
> 5 + 3
8
> const name = "Alice"
undefined
> `Hello ${name}!`
'Hello Alice!'
> .exit // or Ctrl+C twice
This is great for:
- Testing code snippets
- Learning JavaScript syntax
- Quick calculations
- Experimenting with Node APIs
Node Global Objects
NodeJS provides several global objects (available everywhere):
// Process information
console.log(process.version); // Node version
console.log(process.cwd()); // Current working directory
console.log(process.env); // Environment variables
// Timers (same as browser)
setTimeout(() => console.log("Later!"), 1000);
setInterval(() => console.log("Tick"), 1000);
// File path info
console.log(__filename); // Current file path
console.log(__dirname); // Current directory path
Running Scripts with Arguments
You can pass arguments to your Node scripts:
// args.js
console.log("Arguments:", process.argv);
// First two are always:
// [0] = path to node executable
// [1] = path to script file
// [2+] = your arguments
const userArgs = process.argv.slice(2);
console.log("User arguments:", userArgs);
Run it:
node args.js hello world 123
Output:
Arguments: ['/usr/local/bin/node', '/path/to/args.js', 'hello', 'world', '123']
User arguments: ['hello', 'world', '123']
Common Node Commands
# Run a script
node script.js
# Run with environment variable
NODE_ENV=production node server.js
# Check Node version
node --version
# Check npm version
npm --version
# Start interactive REPL
node
# Run script and watch for changes (requires nodemon)
npx nodemon script.js
Next Steps
Now that you have NodeJS installed, you’ll learn about:
- How modules work in Node
- Using npm to install packages
- Working with the file system
- Creating web servers
Next Article: Modules and Require