Installing Node.js and npm
Node.js comes with npm (Node Package Manager) bundled in. The recommended way to install Node.js is from the official website nodejs.org or by using a version manager like nvm (Node Version Manager) which allows you to switch between multiple versions.
After installation, you can verify everything is set up correctly by checking the versions in your terminal. The LTS (Long Term Support) version is recommended for production use.
# Check if Node.js is installed
node --version
# v20.11.0
# Check npm version
npm --version
# 10.2.4
# Run a JavaScript file with Node.js
node app.js
# Run JavaScript directly in the REPL
node
> 2 + 2
4
> .exit - Download from nodejs.org — Choose the LTS version for stability
- nvm (Node Version Manager) — Install and switch between multiple Node.js versions
- npm — Comes bundled with Node.js, manages packages and dependencies
- REPL — Interactive Node.js shell for quick testing (type 'node' in terminal)
- Use nvm to manage multiple Node.js versions. Install it from github.com/nvm-sh/nvm (Linux/Mac) or nvm-windows for Windows.
Creating a Project with package.json
Every Node.js project starts with a package.json file. This file contains metadata about your project — its name, version, dependencies, and scripts. You create it by running npm init in your project directory.
The package.json file is the heart of any Node.js project. It tracks which packages your project depends on and defines scripts for common tasks like starting your app or running tests.
# Create a new project directory
mkdir my-node-app
cd my-node-app
# Initialize with interactive prompts
npm init
# Or skip prompts with defaults
npm init -y
# The generated package.json looks like:
{
"name": "my-node-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js",
"dev": "node --watch index.js",
"test": "echo \"No tests\" && exit 1"
},
"keywords": [],
"license": "ISC"
} - npm init — Interactive setup for package.json
- npm init -y — Quick setup with all defaults
- main — The entry point file for your application
- scripts — Custom commands you can run with npm run
- npm start — Runs the 'start' script (no 'run' needed for start/test)
- Starting with Node.js 18, you can use --watch flag to auto-restart your app on file changes without installing nodemon.
