Node.js NPM & Package Management — Questions and Answers
Question 1: What is the purpose of package.json in a Node.js project?
- To define project metadata, dependencies, scripts, and configuration (Correct answer)
- To store application runtime data in JSON format
- To configure the Node.js runtime environment settings
- To define database schemas in JSON
Correct answer: To define project metadata, dependencies, scripts, and configuration
package.json serves as the manifest file for a Node.js project, defining project metadata, dependencies, devDependencies, scripts, version, license, and other configuration.
Question 2: What is the difference between dependencies and devDependencies in package.json?
- Dependencies are needed at runtime; devDependencies are only needed during development (Correct answer)
- There is no practical difference between them
- DevDependencies are installed globally; dependencies are local
- Dependencies are optional; devDependencies are required
Correct answer: Dependencies are needed at runtime; devDependencies are only needed during development
Dependencies are packages required for the application to run in production, while devDependencies are only needed during development (testing tools, linters, build tools).
Question 3: What does 'npm install --save-exact' do?
- Installs the package and saves the exact version number without any range prefix (Correct answer)
- Installs the package exactly once, preventing duplicates
- Saves the exact file size of the installed package
- Installs only the exact files needed, excluding documentation
Correct answer: Installs the package and saves the exact version number without any range prefix
--save-exact pins the installed package to its exact version number (e.g., 1.2.3 instead of ^1.2.3), preventing automatic updates to newer versions.
Question 4: What is the purpose of package-lock.json?
- To lock the exact versions of all installed dependencies and their sub-dependencies (Correct answer)
- To prevent unauthorized access to the package registry
- To lock the project so no new packages can be added
- To store encrypted passwords for npm registry access
Correct answer: To lock the exact versions of all installed dependencies and their sub-dependencies
package-lock.json records the exact version of every installed package and its dependency tree, ensuring consistent installations across different environments and machines.
Question 5: What is semantic versioning (semver) in npm?
- A versioning scheme using MAJOR.MINOR.PATCH where each number indicates the type of change (Correct answer)
- A version naming system using alphabetical codenames
- A system that versions packages by date of release
- A random version assignment system used by npm
Correct answer: A versioning scheme using MAJOR.MINOR.PATCH where each number indicates the type of change
Semantic versioning uses three numbers (MAJOR.MINOR.PATCH): MAJOR for breaking changes, MINOR for backward-compatible features, and PATCH for backward-compatible bug fixes.
Question 6: What does the caret (^) prefix mean in a package.json version?
- Allow updates that do not modify the left-most non-zero digit (Correct answer)
- Install exactly this version and nothing else
- Install any version greater than this number
- The package is deprecated and should not be used
Correct answer: Allow updates that do not modify the left-most non-zero digit
The caret (^) allows updates to MINOR and PATCH versions while keeping the MAJOR version fixed (e.g., ^1.2.3 allows 1.x.x but not 2.0.0), following semver compatibility.
What is the purpose of package.json in a Node.js project?