Beginner's Guide to Node.js and NPM
This guide explains how to install and manage packages in Node.js projects using npm, with practical commands you will use every day.
Prerequisites
- Node.js installed (npm is included)
- VS Code installed
- Terminal access (PowerShell, Command Prompt, Git Bash, macOS/Linux terminal)
Check your versions:
node -v
npm -v
1. Initialize a project
Before installing packages, create a package.json file.
npm init -y
What this does:
- Creates
package.jsonwith default values - Registers project metadata and dependencies
2. Install dependencies
2.1 Runtime dependency
Use this for libraries required in production.
npm install dayjs
Short form:
npm i dayjs
Result:
- Adds package to
dependenciesinpackage.json - Creates/updates
package-lock.json - Downloads modules into
node_modules/
2.2 Development dependency
Use this for tooling (linters, test runners, watchers, etc.).
npm install nodemon --save-dev
Short form:
npm i -D nodemon
Result:
- Adds package to
devDependencies
3. Use an installed library
Create index.js:
const dayjs = require('dayjs');
const now = dayjs().format('YYYY-MM-DD HH:mm:ss');
console.log('Current time:', now);
Run it:
node index.js
4. Global packages
Install globally only when you need a CLI command available system-wide.
npm install -g typescript
warning
Avoid global installs for project-specific tooling. Prefer local dev dependencies plus npx.
5. Essential npm commands
Remove a package
npm uninstall dayjs
Update packages (within allowed semver range)
npm update
Install dependencies from package.json
npm install
Use this after cloning a repository.
6. Useful VS Code workflow
- Open the integrated terminal (`Ctrl + ``) in the project root.
- Use the
NPM SCRIPTSpanel to run scripts frompackage.json. - Use IntelliSense for
import/requiresuggestions from installed packages.
Quick reference
| Goal | Command | Short form |
|---|---|---|
| Initialize project | npm init -y | - |
| Install dependency | npm install <name> | npm i <name> |
| Install dev dependency | npm install <name> --save-dev | npm i -D <name> |
| Install global package | npm install -g <name> | - |
| Remove dependency | npm uninstall <name> | npm un <name> |
| Install from lockfile/package.json | npm install | npm i |
| Run script | npm run <script> | - |
Common mistakes
- Committing
node_modules/to Git. - Installing everything globally.
- Forgetting to run
npm installafter cloning a project. - Editing
package-lock.jsonmanually.
Next steps
- Learn
package.jsonscripts (build,dev,test). - Learn
npxfor running local binaries. - Add linting and formatting (
eslint,prettier) as dev dependencies.