Node.js NPM & Package Management 3 — Questions and Answers
Question 1: Which npm script lifecycle hook runs automatically BEFORE the `build` script?
- prebuild (Correct answer)
- beforebuild
- setup
- preinstall
Correct answer: prebuild
npm automatically runs a script named `prebuild` before the `build` script, following the `pre<scriptname>` convention.
Question 2: What does the caret (`^`) range in `"express": "^4.18.0"` allow?
- Only the exact version 4.18.0
- Any version >=4.18.0 and <5.0.0 (Correct answer)
- Any version >=4.0.0
- Any version >=4.18.0 and <4.19.0
Correct answer: Any version >=4.18.0 and <5.0.0
The caret allows compatible updates: it pins the major version and allows minor and patch updates, so `>=4.18.0 <5.0.0`.
Question 3: How do you run a locally installed CLI tool (in node_modules/.bin) without adding it to PATH?
- npm exec
- npx (Correct answer)
- node_modules run
- npm start
Correct answer: npx
`npx <tool>` executes a binary from node_modules/.bin (or downloads it temporarily) without needing a global install.
Question 4: What command publishes a package to the npm registry?
- npm push
- npm deploy
- npm publish (Correct answer)
- npm release
Correct answer: npm publish
`npm publish` uploads the package to the registry, making it available for others to install.
Question 5: Which field in package.json specifies the entry point file of a Node.js module?
- start
- index
- main (Correct answer)
- entry
Correct answer: main
The `main` field defines the file that is loaded when the package is `require()`d by another module.
Question 6: What does `npm link` do?
- Creates a symlink from node_modules to a local package for development (Correct answer)
- Links two npm accounts together
- Publishes a package under an alias
- Installs a package from a git URL
Correct answer: Creates a symlink from node_modules to a local package for development
`npm link` creates a global symlink for a local package, then `npm link <pkg>` in another project symlinks to it for local development testing.
Question 7: What does the `engines` field in package.json specify?
- The build tools required
- The compatible Node.js and npm version ranges the package supports (Correct answer)
- The operating systems the package runs on
- The JavaScript engine internals used
Correct answer: The compatible Node.js and npm version ranges the package supports
The `engines` field declares which Node.js (and optionally npm) versions the package is compatible with, and npm warns if the running version doesn't match.
Which npm script lifecycle hook runs automatically BEFORE the `build` script?