Node.js NPM & Package Management 2 — Questions and Answers
Question 1: What does the `--save-exact` flag do when running `npm install`?
- Saves the package to devDependencies
- Pins the exact version instead of using a caret range (Correct answer)
- Installs without updating package-lock.json
- Forces a fresh install ignoring cache
Correct answer: Pins the exact version instead of using a caret range
`--save-exact` records the precise installed version (e.g., `1.2.3`) rather than a range like `^1.2.3`.
Question 2: Which npm command removes a package from node_modules AND from package.json?
- npm delete
- npm remove
- npm uninstall (Correct answer)
- npm purge
Correct answer: npm uninstall
`npm uninstall <pkg>` removes the package from node_modules and updates package.json and package-lock.json.
Question 3: What is the purpose of `npm ci` compared to `npm install`?
- It installs only CI-specific packages
- It installs from package-lock.json exactly, deleting node_modules first (Correct answer)
- It creates a new package-lock.json
- It checks for security vulnerabilities
Correct answer: It installs from package-lock.json exactly, deleting node_modules first
`npm ci` deletes node_modules then installs exactly what package-lock.json specifies, ensuring reproducible builds.
Question 4: Which file should you commit to version control to lock dependency versions for a library package?
- package-lock.json only
- Both package.json and package-lock.json (Correct answer)
- package.json only
- shrinkwrap.json
Correct answer: Both package.json and package-lock.json
Both package.json and package-lock.json should be committed; package-lock.json ensures reproducible installs across environments.
Question 5: How do you view all globally installed npm packages?
- npm list
- npm list -g --depth=0 (Correct answer)
- npm global list
- npm show -g
Correct answer: npm list -g --depth=0
`npm list -g --depth=0` shows top-level globally installed packages without listing their sub-dependencies.
Question 6: What does `npm outdated` report?
- Packages with security vulnerabilities
- Packages where the installed version is behind the wanted or latest version (Correct answer)
- Packages not listed in package.json
- Deprecated packages
Correct answer: Packages where the installed version is behind the wanted or latest version
`npm outdated` compares installed, wanted (range-satisfying), and latest versions to show which packages can be updated.
Question 7: What is the effect of setting `"private": true` in package.json?
- Hides the package from npm search
- Prevents the package from being accidentally published to the npm registry (Correct answer)
- Restricts install to private networks only
- Enables scoped package resolution
Correct answer: Prevents the package from being accidentally published to the npm registry
`"private": true` causes `npm publish` to refuse to publish the package, protecting internal projects.
What does the `--save-exact` flag do when running `npm install`?