Day 18 - JavaScript Environment Setup
JavaScript Environment Setup
Installing Node.js & Development Tools ๐
CSCI 3403 - Web Programming
Week 9, Class 18
Let's get your JavaScript environment ready!
Today's Learning Objectives
- ๐ฏ Understand what Node.js is and why we need it
- ๐ฏ Install NVM (Node Version Manager)
- ๐ฏ Install Node.js using NVM
- ๐ฏ Learn the difference between NVM and NPM
- ๐ฏ Run the Node console (REPL)
- ๐ฏ Understand deployment considerations
What is Node.js? ๐ค
JavaScript Outside the Browser!
Browser JavaScript
- Runs in Chrome, Firefox, etc.
- Access to DOM
- window object
- Client-side only
Node.js JavaScript
- Runs on your computer
- Access to file system
- process object
- Server-side capable
Think of it this way: Node.js lets JavaScript escape the browser sandbox!
Why Do We Need Node.js? ๐ก
Modern Web Development Requirements
- ๐ฆ Package Management: Install and manage libraries (React, Vue, etc.)
- ๐ ๏ธ Build Tools: Compile, bundle, and optimize your code
- ๐งช Testing: Run automated tests for your JavaScript
- ๐ฅ๏ธ Server Development: Build APIs and backend services
- โก Development Tools: Live reload, linting, formatting
Coming up: We'll use Node.js for React, build tools, and more!
Understanding the Tools ๐ง
NVM vs Node vs NPM - What's What?
Don't Confuse: NVM manages Node versions. NPM manages packages. They sound similar but do different things!
Installation Strategy ๐
Why Use NVM Instead of Direct Installation?
โ
With NVM
- Switch Node versions easily
- Test on multiple versions
- No permission issues
- Project-specific versions
- Easy upgrades
โ Without NVM
- Stuck with one version
- Complex upgrade process
- Potential permission problems
- System-wide installation only
- Harder to troubleshoot
Pre-Installation Check ๐
Do You Already Have Node?
Let's check if you already have Node.js installed:
$ node --version
-bash: node: command not found
$ which node
# No output means not installed
If you see a version number: You have Node! But we'll still install NVM for better management.
Installing NVM on Linux ๐ง
Step 1: Download and Install Script
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
Step 2: Add to Shell Profile
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
Step 3: Reload Configuration
source ~/.bashrc
Ubuntu/Debian Users: You might need to install curl first: sudo apt install curl
Installing NVM on macOS ๐
Step 1: Install Homebrew (if needed)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Step 2: Install NVM via Homebrew
brew install nvm
Step 3: Create NVM Directory & Configure
mkdir ~/.nvm
echo 'export NVM_DIR="$HOME/.nvm"' >> ~/.zshrc
echo '[ -s "/opt/homebrew/opt/nvm/nvm.sh" ] && \. "/opt/homebrew/opt/nvm/nvm.sh"' >> ~/.zshrc
source ~/.zshrc
Note: macOS uses zsh by default. If using bash, replace .zshrc with .bash_profile
Verifying NVM Installation โ
Make Sure Everything Works!
$ nvm --version
0.39.0
$ command -v nvm
nvm
$ nvm list
No versions installed yet
Troubleshooting:
- If "command not found": Close and reopen terminal
- Still not working? Run:
source ~/.bashrc
or source ~/.zshrc
Installing Node.js with NVM ๐ฅ
Step 1: Install LTS Version
$ nvm install --lts
Downloading and installing node v20.11.0...
##################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v20.11.0 (npm v10.2.4)
What is LTS?
Long Term Support - Stable version supported for 30 months. Best for production!
Setting Your Node Version โ๏ธ
Step 2: Configure Default Version
$ nvm use --lts
Now using node v20.11.0 (npm v10.2.4)
$ nvm alias default lts/*
default -> lts/* (-> v20.11.0)
$ node --version
v20.11.0
$ npm --version
10.2.4
Success! You now have Node.js and npm installed! ๐
Essential NVM Commands ๐
Your NVM Cheat Sheet
nvm list
nvm list-remote
nvm install 18.19.0
nvm use 18.19.0
nvm alias default 20.11.0
nvm uninstall 16.14.0
Pro Tip: Create a .nvmrc
file in your project with the Node version!
Using the Node Console ๐ป
Interactive JavaScript in Your Terminal!
$ node
Welcome to Node.js v20.11.0.
Type ".help" for more information.
> console.log("Hello, Node!")
Hello, Node!
> 2 + 2
4
> const name = "Student"
> `Hello, ${name}!`
'Hello, Student!'
> .exit
$
REPL = Read-Eval-Print Loop: Perfect for testing small code snippets!
Node REPL Special Commands ๐ฎ
.exit
.help
.save mySession.js
.load myCode.js
.clear
.break
Try This:
Open Node console and create a simple function to calculate area of a circle!
Let's Practice! ๐โโ๏ธ
10-Minute Setup Challenge
- Verify NVM is installed:
nvm --version
- Install Node LTS:
nvm install --lts
- Set as default:
nvm use --lts
- Check versions:
node -v
and npm -v
- Open Node REPL:
node
- Create a function that reverses a string
- Test with:
reverseString("hello")
- Exit with
.exit
Troubleshooting Common Issues ๐ง
Issue 1: "nvm: command not found"
Solution: Source your profile file or restart terminal
source ~/.bashrc
or
source ~/.zshrc
Issue 2: "Permission denied" errors with npm
Solution: Never use sudo with npm! NVM prevents this issue.
Issue 3: Node version keeps resetting
Solution: Set a default: nvm alias default node
Golden Rule: When in doubt, close terminal and reopen!
Quick NPM Preview ๐ฆ
Your Package Manager is Ready!
NPM came with Node.js - you already have it!
npm --version
npm init -y
npm install lodash
npm install --save-dev eslint
npm run start
Coming Soon: We'll dive deep into NPM when we start building projects!
Node.js: Development Superpower ๐ช
How Node Makes Development Easier
โจ Development Benefits
- One language everywhere (JavaScript)
- Huge ecosystem (NPM packages)
- Fast development cycles
- Great for APIs and microservices
- Excellent tooling support
- Hot reloading & live development
- Easy to get started
๐ What You Can Build
- REST APIs
- Real-time applications
- Command-line tools
- Build tools & bundlers
- Desktop apps (Electron)
- Mobile apps (React Native)
- Serverless functions
But... Deployment Gets Interesting ๐
The Other Side of the Coin
โ ๏ธ Deployment Considerations
While Node.js makes development smooth, deployment can be... educational:
๐ Learning Opportunities
- Managing dependencies in production
- Handling different Node versions
- Process management (PM2, forever)
- Memory management concerns
- Security updates & patches
- Scaling considerations
๐ Growth Areas
- DevOps skills become important
- Docker containers often needed
- Monitoring becomes crucial
- Error handling is critical
- Performance tuning required
- Infrastructure knowledge helps
The Good News: These challenges make you a better developer!
Every deployment issue is a learning opportunity.
The Professional Perspective ๐
Growing Into Production-Ready Development
The Journey from Local to Production
"It works on my machine!" โ "It works in production!"
๐ฑ Starting Out
- npm install everything
- node_modules = 500MB
- Works locally perfectly
- "Why is deployment hard?"
๐ณ Growing Experience
- Understanding package-lock.json
- Using environment variables
- Learning about CI/CD
- Containerization makes sense
Remember: Every senior developer was once confused by deployment.
The complexity is what makes it valuable to learn!
Setting Yourself Up for Success ๐ฏ
Node.js Best Practices from Day One
- โ
Always use NVM for version management
- โ
Stick to LTS versions for important projects
- โ
Commit package-lock.json to git
- โ
Never commit node_modules/
- โ
Use .nvmrc files in projects
- โ
Learn about environment variables early
- โ
Understand the difference between dependencies and devDependencies
Pro Tip: Create a .gitignore file with node_modules/ as the first line in every project!
What's Coming Next ๐
Your Node.js Journey Continues
Upcoming Topics:
- ๐ฆ Deep dive into NPM and package.json
- โ๏ธ Installing and using React
- ๐ ๏ธ Build tools (Webpack, Vite)
- ๐งช Testing with Jest
- ๐ฅ๏ธ Creating a Node.js server
- ๐ Working with APIs
Homework: Create a simple Node.js script that reads a file and counts words!
Today's Takeaways ๐
You Can Now:
- โ
Explain what Node.js is and why we need it
- โ
Install and manage Node versions with NVM
- โ
Use the Node REPL for testing code
- โ
Understand the ecosystem (NVM, Node, NPM)
- โ
Troubleshoot common installation issues
Quick Check:
Run node -v
and npm -v
- You should see version numbers!
Next Class: We'll start building with NPM packages!
1 / 24