npm & Semver Questions with Answers

17 practice questions on npm & Semver (Node.js & Backend APIs) with answers and step-by-step solutions. Try 10 samples below, then practise all 17 free in the AptiNest app.

Sample questions

Question 1Easy

Where should a test framework such as Jest be listed in package.json?

  1. dependencies
  2. peerDependencies
  3. devDependencies
  4. bundledDependencies

Answer: C. devDependencies

Explanation

devDependencies are needed only for development and testing, not by the running application.

Step-by-step solution

  1. A test framework is needed only while developing and testing the project.
  2. Production installs should not include it.
  3. So it belongs in devDependencies, which is option C.

Why the other options are wrong

  • A. Dependencies is installed in production and would bloat it.
  • B. PeerDependencies declares a host package the library expects, which is not the case for Jest.
  • D. BundledDependencies is for packages shipped inside a published package.

Tip: Build tools, linters and test frameworks go in devDependencies; runtime code goes in dependencies.

Question 2Easy

What does npm audit do?

  1. Checks your code for style errors
  2. Measures how long installs take
  3. Removes packages from node_modules that are not listed in package.json
  4. Reports known vulnerabilities in the dependency tree

Answer: D. Reports known vulnerabilities in the dependency tree

Explanation

It compares installed versions with a vulnerability database; npm audit fix tries compatible upgrades.

Step-by-step solution

  1. Npm audit checks for security issues in dependencies.
  2. It compares installed packages against a known vulnerabilities list.
  3. The command reports any weaknesses found in the project's dependency tree.
  4. This helps identify risks before they can be exploited.
  5. The correct answer is Reports known vulnerabilities in the dependency tree.

Why the other options are wrong

  • A. Style errors are checked by a linter, not by npm audit.
  • B. Install timing is not measured by npm audit; it only reports security issues.
  • C. Removing extraneous packages is a different job, such as npm prune, not an audit.

Tip: Use npm audit to find and fix security issues in your Node.js projects.

Question 3Easy

Which versions does the range ^1.2.3 allow?

  1. >=1.2.3 and <2.0.0
  2. >=1.2.3 and <1.3.0
  3. Exactly 1.2.3
  4. Any version at or above 1.2.3

Answer: A. >=1.2.3 and <2.0.0

Explanation

The caret allows changes that do not modify the leftmost non-zero part, here the major version 1.

Step-by-step solution

  1. A caret range ^1.2.3 allows changes that do not alter the leftmost non-zero digit.
  2. For 1.2.3 the leftmost non-zero digit is the major number 1.
  3. So the range is >=1.2.3 and <2.0.0, which is option A.

Why the other options are wrong

  • B. This is the tilde range ~1.2.3, which allows only patch changes.
  • C. Exact versions are written without a range symbol.
  • D. This is an open-ended range; the caret range stops before 2.0.0.

Tip: Caret ^ keeps the leftmost non-zero number fixed; tilde ~ keeps the minor number fixed.

Question 4Easy

Should the node_modules folder be committed to Git?

  1. Yes, so installs are not needed
  2. Yes, for packages listed in dependencies
  3. No; it is rebuilt from package.json and the lockfile
  4. Only on the main branch

Answer: C. No; it is rebuilt from package.json and the lockfile

Explanation

node_modules is large and platform-specific; the lockfile already makes the install reproducible.

Step-by-step solution

  1. Node_modules contains all installed packages, which can be very large.
  2. package.json and lockfile already define exact versions of dependencies.
  3. Reinstalling from package.json ensures consistent environment across platforms.
  4. Committing node_modules would cause conflicts and bloat the repository.
  5. Best practice is to exclude node_modules and let it be rebuilt as needed.

Why the other options are wrong

  • A. Committing node_modules bloats the repo; installs still run on other machines to create it.
  • B. Listing packages in dependencies does not mean their installed files belong in Git.
  • D. Branch choice does not decide this; node_modules should be ignored on every branch.

Tip: Never commit node_modules; it's better to reinstall from package.json.

Question 5Medium

What is package-lock.json for?

  1. Stopping other users from editing package.json
  2. Listing the scripts npm can run
  3. Encrypting the package before publishing
  4. Recording exact installed versions for repeatable installs

Answer: D. Recording exact installed versions for repeatable installs

Explanation

Ranges in package.json can resolve differently over time; the lockfile pins the full tree, so it should be committed.

Step-by-step solution

  1. Package-lock.json records exact versions of installed packages.
  2. This ensures same versions are used when installing again.
  3. It makes installs repeatable and avoids version conflicts.
  4. Exact versions are needed for consistent builds.
  5. So the answer is Recording exact installed versions for repeatable installs.

Why the other options are wrong

  • A. Lockfiles do not control who may edit package.json; access is a repository matter.
  • B. Scripts are listed in package.json under the scripts field, not in the lockfile.
  • C. Packages are not encrypted before publishing; the lockfile is only a record of installs.

Tip: Always commit package-lock.json to maintain build consistency.

Question 6Medium

Which version satisfies ^2.4.0?

  1. 2.9.1
  2. 3.0.0
  3. 2.3.9
  4. 1.9.9

Answer: A. 2.9.1

Explanation

^2.4.0 means >=2.4.0 and <3.0.0. 2.3.9 is too old and 3.0.0 is a new major version.

Step-by-step solution

  1. ^2.4.0 means at least 2.4.0 and below 3.0.0.
  2. Check 2.9.1: it is at least 2.4.0 and below 3.0.0, so it fits.
  3. The only fitting version is 2.9.1, which is option A.

Why the other options are wrong

  • B. 3.0.0 is not below 3.0.0, so it is outside the range.
  • C. 2.3.9 is lower than 2.4.0.
  • D. 1.9.9 is below the major version 2.

Tip: For ^x.y.z, the version must be at least x.y.z and have the same major number x.

Question 7Medium

How does npm ci differ from npm install?

  1. It installs exactly from the lockfile and fails on a mismatch
  2. It updates every package to its latest version
  3. It installs only devDependencies
  4. It ignores the lockfile and resolves package.json ranges afresh

Answer: A. It installs exactly from the lockfile and fails on a mismatch

Explanation

npm ci is meant for CI: it deletes node_modules, never rewrites package-lock.json and errors if the two files disagree.

Step-by-step solution

  1. Npm ci uses the lockfile to install exact versions.
  2. It fails if the lockfile doesn't match package.json.
  3. Npm install can update packages beyond the lockfile.
  4. So npm ci ensures consistency by failing on mismatch.
  5. The correct answer is A.

Why the other options are wrong

  • B. npm ci does not update packages; it installs what the lockfile records.
  • C. npm ci installs all dependencies, including devDependencies, not only dev ones.
  • D. npm ci does not ignore the lockfile; it is the lockfile that drives its install.

Tip: Use npm ci in CI to ensure exact dependencies and avoid mismatches.

Question 8Medium

package.json has "scripts": { "test": "jest", "build": "tsc" }. Which pair of commands runs them?

  1. npm test and npm run build
  2. npm test and npm build
  3. npm run-test and npm run-build
  4. node test and node build

Answer: A. npm test and npm run build

Explanation

start, test and a few others have shortcuts; custom scripts such as build need npm run.

Step-by-step solution

  1. Npm test runs the test script because test is a shortcut.
  2. Npm run build runs the build script because it's custom.
  3. Npm test is correct for the test command.
  4. Npm run build is correct for the build command.
  5. So the pair is npm test and npm run build.
  6. Final answer: A (npm test and npm run build).

Why the other options are wrong

  • B. npm build is not a built-in command; the build script must be run with npm run build.
  • C. npm run-test is not valid syntax; the script is run with npm test and npm run build.
  • D. node test would look for a file named test, not the script in package.json.

Tip: Use 'npm test' for built-in commands, 'npm run' for custom ones.

Question 9Hard

What does the "exports" field in a package's package.json control?

  1. Which files npm publishes to the registry
  2. Which entry points other code may import
  3. Which environment variables the package reads
  4. Which licence applies to the package

Answer: B. Which entry points other code may import

Explanation

exports maps public subpaths (and import/require variants) to files; unlisted internal paths cannot be imported.

Step-by-step solution

  1. The exports field maps import paths to files in the package.
  2. It decides which entry points other code may import, and it hides other internal files.
  3. So the answer is option B.

Why the other options are wrong

  • A. The files field controls what gets published, not exports.
  • C. Environment variables are not declared in exports.
  • D. The license field states the licence, not exports.

Tip: Use exports to define a package's public entry points; anything not listed is private to the package.

Question 10Hard

Which versions does the range ^0.2.3 allow?

  1. >=0.2.3 and <1.0.0
  2. Exactly 0.2.3
  3. >=0.2.3 and <0.2.4
  4. >=0.2.3 and <0.3.0

Answer: D. >=0.2.3 and <0.3.0

Explanation

For 0.x versions the minor number is the leftmost non-zero part, so the caret behaves like the tilde: minor changes are treated as breaking.

Step-by-step solution

  1. Caret ^0.2.3 allows any version starting from 0.2.3.
  2. It includes all versions up to but not including 0.3.0.
  3. This means it covers >=0.2.3 and <0.3.0.
  4. Option D matches this range exactly.
  5. Final answer: >=0.2.3 and <0.3.0.

Why the other options are wrong

  • A. Caret on a 0.x version locks the minor number, so versions up to 0.3.0 are not included.
  • B. A caret range is not an exact pin; 0.2.4 is allowed within the range.
  • C. A caret does not fix the patch number, so 0.2.4 is still allowed.

Tip: Use ^ for safe updates within a major version range.

Practise all 17 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Node.js & Backend APIs topics · All topics