Node.js NPM & Package Management 5 — Questions and Answers
Question 1: What does `npm pack` do?
- Bundles node_modules into a zip
- Creates a tarball (.tgz) of the package as it would be published (Correct answer)
- Compresses package-lock.json
- Archives old package versions
Correct answer: Creates a tarball (.tgz) of the package as it would be published
`npm pack` generates a `.tgz` tarball identical to what `npm publish` would upload, useful for local testing before publishing.
Question 2: Which npm workspaces command installs dependencies for all workspace packages at once?
- npm install --workspaces
- npm install -ws
- npm workspaces install
- Both A and B are valid (Correct answer)
Correct answer: Both A and B are valid
Both `npm install --workspaces` and `npm install -ws` install dependencies for all packages in the workspaces, as `-ws` is the shorthand flag.
Question 3: What does the `files` field in package.json control?
- Source files included in the git repository
- Files included in the published npm package (Correct answer)
- Files watched during development
- Files processed by the build tool
Correct answer: Files included in the published npm package
The `files` field is a whitelist of files and directories to include in the published tarball; it takes precedence over `.npmignore`.
Question 4: What is the difference between `dependencies` and `devDependencies`?
- `devDependencies` are installed first
- `dependencies` are needed at runtime; `devDependencies` are only needed during development/testing (Correct answer)
- `dependencies` cannot be updated; `devDependencies` can
- There is no practical difference in Node.js apps
Correct answer: `dependencies` are needed at runtime; `devDependencies` are only needed during development/testing
`dependencies` are required to run the application in production, while `devDependencies` (like test frameworks or build tools) are only needed locally.
Question 5: What does `npm version patch` do?
- Displays the current patch version
- Increments the patch segment of the version and creates a git tag (Correct answer)
- Applies security patches from npm audit
- Rolls back to the previous patch version
Correct answer: Increments the patch segment of the version and creates a git tag
`npm version patch` bumps the patch number (e.g., `1.0.0` → `1.0.1`), updates package.json, and creates a git commit and tag.
Question 6: Which config option sets the default registry for npm?
- npm config set source
- npm config set registry (Correct answer)
- npm config set origin
- npm set default-registry
Correct answer: npm config set registry
`npm config set registry <url>` updates the `.npmrc` registry value used for all package lookups by default.
Question 7: What happens when you run `npm install` with an existing package-lock.json but no node_modules?
- npm ignores package-lock.json and resolves fresh versions
- npm installs exact versions from package-lock.json (Correct answer)
- npm deletes package-lock.json and regenerates it
- npm throws an error requiring `npm ci` instead
Correct answer: npm installs exact versions from package-lock.json
With a package-lock.json present, `npm install` uses it to install the locked versions, ensuring consistent installs across machines.