๐ฏ Today's Goal: Install Node.js and set up your JavaScript development environment
๐ค What is Node.js?
Node.js is a JavaScript _______________ environment that allows you to run JavaScript _______________ of your web browser.
Browser JavaScript vs Node.js
Browser JavaScript |
Node.js |
Runs in _______________ |
Runs on _______________ |
Access to DOM |
Access to _______________ |
Has window object |
Has _______________ object |
Client-side only |
_______________-side capable |
๐ก Remember: Node.js lets JavaScript escape the browser sandbox!
๐ก Why Do We Need Node.js?
List 5 reasons why modern web development requires Node.js:
- ๐ฆ _________________________________________
- ๐ ๏ธ _________________________________________
- ๐งช _________________________________________
- ๐ฅ๏ธ _________________________________________
- โก _________________________________________
๐ง Understanding the Tools
NVM vs Node vs NPM - Fill in the descriptions:
Tool |
Full Name |
Purpose |
NVM ๐ |
Node _______________ Manager |
_________________________________________ |
Node.js โ๏ธ |
JavaScript Runtime |
_________________________________________ |
NPM ๐ฆ |
Node _______________ Manager |
_________________________________________ |
โ ๏ธ Don't Confuse: NVM manages _______________ versions.
NPM manages _______________. They sound similar but do different things!
๐ Why Use NVM Instead of Direct Installation?
List 3 advantages of using NVM:
- _________________________________________
- _________________________________________
- _________________________________________
๐ Pre-Installation Check
Check if Node is already installed by running these commands:
$ node _______________
$ which _______________
If you see "command not found", Node is _______________ installed.
If you see a version number like v20.11.0, Node _______________ installed.
๐ป Installation Steps
Choose Your Operating System:
โ Linux โ macOS โ Windows (WSL)
Step 1: Install NVM
For Linux:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
For macOS (with Homebrew):
brew install nvm
โ
Check: After installation, close and reopen your terminal!
Step 2: Verify NVM Installation
$ nvm --version
You should see a version number like: _______________
Step 3: Install Node.js
$ nvm install _______________
What does LTS stand for? _________________________________________
Step 4: Set Node Version
$ nvm use _______________
$ nvm alias default _______________
Step 5: Verify Installation
Record your versions:
- Node version (node -v): _______________
- NPM version (npm -v): _______________
๐ Essential NVM Commands
Complete the command descriptions:
Command |
What it does |
nvm list |
_________________________________________ |
nvm list-remote |
_________________________________________ |
nvm install 18.19.0 |
_________________________________________ |
nvm use 18.19.0 |
_________________________________________ |
nvm alias default 20.11.0 |
_________________________________________ |
๐ป The Node Console (REPL)
REPL stands for: _______________-_______________-_______________ Loop
REPL Commands:
- Start REPL:
node
- Exit REPL:
_______________
- Show help:
_______________
- Save session:
_______________ filename.js
- Clear context:
_______________
๐ Practice Exercise:
In the Node REPL, create a function that reverses a string:
function reverseString(str) {
// Your code here:
}
๐ง Common Issues & Solutions
Problem |
Solution |
"nvm: command not found" |
_________________________________________ |
"Permission denied" with npm |
Never use _______________ with npm! |
Node version keeps resetting |
Set a default: nvm alias _______________ node |
๐ก Golden Rule: When in doubt, _________________________________________!
๐ฆ Quick NPM Preview
Complete these common NPM commands:
- Initialize new project:
npm init _______________
- Install a package:
npm _______________ package-name
- Install dev dependency:
npm install _______________ package-name
- Run a script:
npm _______________ script-name
๐ Development vs Deployment
Node.js Development Benefits:
List 3 ways Node.js makes development easier:
- _________________________________________
- _________________________________________
- _________________________________________
Deployment Considerations:
List 3 challenges you might face when deploying Node.js applications:
- _________________________________________
- _________________________________________
- _________________________________________
โ ๏ธ Remember: "It works on my machine!" โ "It works in production!" requires careful planning!
โ
Best Practices Checklist
Check off as you implement each practice:
- Always use NVM for version management
- Stick to LTS versions for important projects
- Commit package-lock.json to git
- Never commit _______________ folder
- Use .nvmrc files in projects
- Learn about environment variables early
- Understand dependencies vs devDependencies
๐ Review Questions
- What is the main difference between Node.js and browser JavaScript?
_________________________________________
_________________________________________
- Why should we use NVM instead of installing Node directly?
_________________________________________
_________________________________________
- What's the difference between NVM and NPM?
_________________________________________
_________________________________________
- What command exits the Node REPL?
_______________
- What does LTS stand for and why is it important?
_________________________________________
_________________________________________
๐ Additional Notes
Use this space for any additional notes, questions, or observations:
๐ Homework
Create a simple Node.js script that reads a file and counts words!
Requirements:
- Create a new file called
wordCounter.js
- Use Node's built-in
fs
module
- Read a text file
- Count and display the number of words
- Handle errors gracefully