JavaScript Node.js Basics 5 — Questions and Answers
Question 1: What is the purpose of the `package-lock.json` file in a Node.js project?
- It prevents other developers from installing new packages
- It locks the exact versions of all installed dependencies for reproducible installs (Correct answer)
- It lists only the direct dependencies of the project
- It stores npm authentication tokens for private registries
Correct answer: It locks the exact versions of all installed dependencies for reproducible installs
`package-lock.json` records the exact resolved versions and integrity hashes of every package in the dependency tree, ensuring identical installs across environments.
Question 2: What does `npm install --save-dev` do differently from `npm install`?
- It installs packages globally instead of locally
- It installs packages and records them under `devDependencies` in package.json (Correct answer)
- It installs packages without updating package-lock.json
- It installs only the latest major version of each package
Correct answer: It installs packages and records them under `devDependencies` in package.json
`--save-dev` (or `-D`) adds the package to `devDependencies`, signaling it is only needed during development and testing, not in production.
Question 3: Which file do you create to prevent npm from publishing specific files or folders in your package?
- .npmignore (Correct answer)
- .gitignore
- package.ignore
- .publishignore
Correct answer: .npmignore
`.npmignore` works like `.gitignore` but specifically controls which files are excluded from the tarball when you run `npm publish`.
Question 4: What does the `exports` field in `package.json` allow you to define?
- The list of npm scripts available in the package
- Conditional and subpath exports that control which files external consumers can import (Correct answer)
- The entry point for the package's CLI binary
- The peer dependencies required by the package
Correct answer: Conditional and subpath exports that control which files external consumers can import
The `exports` field lets package authors map subpaths and conditions (e.g., `import`, `require`, `browser`) to specific file paths, enabling encapsulation of internal modules.
Question 5: What does Node.js's `util.promisify()` do?
- It converts a Promise into a callback-style function
- It wraps a Node.js-style callback function so it returns a Promise instead (Correct answer)
- It creates a pool of Promise workers for parallel execution
- It validates that a function follows the Promise/A+ specification
Correct answer: It wraps a Node.js-style callback function so it returns a Promise instead
`util.promisify()` takes a function that follows the Node.js error-first callback convention and returns a version that returns a Promise.
Question 6: What is the purpose of `NODE_ENV=production` when running a Node.js application?
- It automatically enables HTTPS on all HTTP servers
- It signals frameworks and libraries to enable optimizations and disable development-only features (Correct answer)
- It restricts the process to a single CPU core for consistent performance
- It prevents `require()` from loading devDependencies
Correct answer: It signals frameworks and libraries to enable optimizations and disable development-only features
Setting `NODE_ENV=production` is a convention that frameworks like Express use to enable caching, disable verbose logging, and apply other production optimizations.
Question 7: Which of the following correctly demonstrates how to use ES module syntax in a Node.js project?
- Set `"type": "commonjs"` in package.json and use `import` statements
- Set `"type": "module"` in package.json and use `import` / `export` statements (Correct answer)
- Rename files to `.mjs` and use `require()` statements
- Use `import` statements only inside files named `index.js`
Correct answer: Set `"type": "module"` in package.json and use `import` / `export` statements
Setting `"type": "module"` in `package.json` makes all `.js` files in the package treated as ES modules, enabling native `import`/`export` syntax.
What is the purpose of the `package-lock.json` file in a Node.js project?