JavaScript outside the browser
Node.js lets you run JavaScript on servers, scripts, and CLI tools.
Installation
| System | Command |
|---|---|
| macOS | brew install node |
| Windows | winget install OpenJS.NodeJS |
| Linux | sudo apt install nodejs npm |
# Verify installation
node --version
npm --version
npm vs pnpm vs yarn
| Manager | Speed | Space | Command |
|---|---|---|---|
| npm | Normal | Normal | npm install |
| pnpm | Fast | Efficient | pnpm install |
| yarn | Fast | Normal | yarn |
๐ก Recommendation: pnpm for new projects.
# Install pnpm
npm install -g pnpm
Create project
# Initialize
mkdir my-project && cd my-project
pnpm init
# Add dependency
pnpm add express
# Add dev dependency
pnpm add -D typescript @types/node
package.json
{
"name": "my-project",
"version": "1.0.0",
"scripts": {
"dev": "node index.js",
"build": "tsc",
"start": "node dist/index.js"
},
"dependencies": {
"express": "^4.18.0"
},
"devDependencies": {
"typescript": "^5.0.0"
}
}
# Run scripts
pnpm dev
pnpm build
Example: Basic server
import express from 'express'
const app = express()
app.get('/', (req, res) => res.send('Hello!'))
app.listen(3000, () => console.log('http://localhost:3000'))
Practice
Useful links
- ๐ Node.js Docs
- ๐ pnpm Docs