Here's a step-by-step guide to installing Node.js on different operating systems:
- Go to the official website: https://nodejs.org
- Choose a version:
- LTS (Long-Term Support) → Recommended for most users.
- Current → Has the latest features but may be unstable.
- Download the installer for your operating system:
- Windows:
.msi
- Mac:
.pkg
- Linux:
.tar.xz
- Windows:
- Run the installer and follow the on-screen instructions.
node -v # Check Node.js version
npm -v # Check npm version
Open the terminal and type this command with your project name:
mkdir my-project && cd my-project
Then, open the project folder with VS Code:
code .
Open a terminal in your project folder (Ctrl + ` in VS Code).
npm init -y
// dependencies
const http = require("http");
// app object - module scaffolding
const app = {};
// configuration
app.config = {
port: 3000,
};
// create server
app.createServer = () => {
const server = http.createServer(app.handleRequest);
server.listen(app.config.port, () => {
console.log(`Server is running on port ${app.config.port}`);
});
};
// handle request response
app.handleRequest = (req, res) => {
res.end("Hello World");
};
// start server
app.createServer();
Modify the package.json
file in your project and update the scripts section:
"scripts": {
"dev": "node index.js",
"build": "node index.js"
},
To run the Node.js server, execute the following command:
npm run dev # Development Mode
npm run build # Production Mode
Open http://localhost:3000/
in a browser to see the output.
Nodemon automatically restarts your Node.js app when file changes are detected.
Open a terminal in your project folder (Ctrl + ` in VS Code).
npm install -g nodemon
Modify the package.json
file in your project and update the scripts section:
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js"
},
Run with:
npm run dev
To debug with Nodemon in VS Code:
- Open VS Code.
- Go to Run and Debug (
Ctrl + Shift + D
). - Click "create a
launch.json
file". - Select "Node.js".
- Replace the content of
launch.json
with:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch with Nodemon",
"runtimeExecutable": "nodemon",
"program": "${workspaceFolder}/app.js",
"restart": true,
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}
Save the file.
- Place a
console.log()
in yourapp.js
file. - Modify and save the file to check if Nodemon automatically restarts.
- Start debugging in VS Code using F5.
Package Name | Description | Install Command |
---|---|---|
express | Web framework for Node.js | npm install express |
nodemon | Auto-restarts server on file changes | npm install -g nodemon |
dotenv | Loads environment variables | npm install dotenv |
cors | Enables CORS | npm install cors |
mongoose | MongoDB ODM | npm install mongoose |
mysql2 | MySQL driver | npm install mysql2 |
pg | PostgreSQL client | npm install pg |
bcrypt | Password hashing | npm install bcrypt |
jsonwebtoken | JWT authentication | npm install jsonwebtoken |
multer | File uploads | npm install multer |
axios | HTTP client | npm install axios |
socket.io | WebSockets | npm install socket.io |
jest | Testing framework | npm install --save-dev jest |
supertest | HTTP testing | npm install --save-dev supertest |
eslint | JavaScript linter | npm install eslint --save-dev |
Sometimes, the installed npm version isn't the latest. Update it:
npm install -g npm@latest
Command | Description |
---|---|
npm install <package> |
Installs a package locally |
npm install -g <package> |
Installs a package globally |
npm uninstall <package> |
Removes a package |
npm update |
Updates all installed packages |
npm list |
Lists installed packages |
npm run <script> |
Runs a script from package.json |
npm audit |
Checks for security vulnerabilities |
nvm install-latest-npm
nvm install node
nvm use node
node -v
nvm install latest
nvm use latest
node -v
brew update
brew upgrade node
node -v
sudo apt update && sudo apt upgrade
sudo apt install nodejs npm
node -v
This project is licensed under the MIT License - see the LICENSE file for details.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.