npm (Node Package Manager) installs dependencies, runs scripts, and publishes packages. Every serious Node project starts with npm init and a package.json manifest.
Essential files
package.json— name, version, scripts, dependenciespackage-lock.json— exact dependency tree for reproducible installsnode_modules/— installed packages (usually gitignored)
Common commands
npm install— install from lock filenpm install express— add dependencynpm run dev— run script from package.jsonnpm audit— report known vulnerabilities
Scripts example
"scripts": {
"start": "node src/main.mjs",
"dev": "node --watch src/main.mjs",
"test": "node --test"
}
Playground note
The runner cannot npm install arbitrary packages—lessons simulate manifests in comments and use built-ins in runnable code.
Important interview questions and answers
- Q: package.json vs package-lock.json?
A: JSON declares semver ranges; lock pins exact versions—commit lock in apps for CI reproducibility. - Q: dependencies vs devDependencies?
A: Runtime vs build/test-only tools (eslint, jest)—production installs can omit dev withnpm ci --omit=dev.
Self-check
- Why commit package-lock.json?
- What folder holds installed packages?
Interview prep
- package.json vs package-lock.json?
JSON declares ranges and scripts; lock pins exact dependency tree for reproducible installs—commit lock in applications.