Node.js Technology & Digital Applications 4 — Questions and Answers
Question 1: Which environment variable is conventionally used to set the Node.js execution environment (e.g., development, production)?
- NODE_ENV (Correct answer)
- APP_MODE
- RUNTIME_ENV
- NODE_MODE
Correct answer: NODE_ENV
`NODE_ENV` is the widely adopted convention used by frameworks like Express to toggle behavior between development and production.
Question 2: What does the `--save-dev` flag do when running `npm install`?
- Saves the package globally
- Adds the package to `devDependencies` in package.json (Correct answer)
- Installs the package without updating package.json
- Locks the package version permanently
Correct answer: Adds the package to `devDependencies` in package.json
`--save-dev` (or `-D`) adds the package under `devDependencies`, indicating it's only needed during development and not in production.
Question 3: In the context of Node.js, what is a 'middleware' function in Express?
- A function that compiles TypeScript before the server starts
- A function with access to req, res, and next that sits in the request-response cycle (Correct answer)
- A function that runs in a separate worker thread
- A function that caches database query results
Correct answer: A function with access to req, res, and next that sits in the request-response cycle
Middleware functions receive `(req, res, next)` and can execute code, modify the request/response, or pass control to the next middleware via `next()`.
Question 4: What is the purpose of the `package-lock.json` file?
- Prevents developers from adding new packages
- Records the exact resolved versions of all dependencies for reproducible installs (Correct answer)
- Locks the Node.js version required for the project
- Encrypts sensitive package metadata
Correct answer: Records the exact resolved versions of all dependencies for reproducible installs
`package-lock.json` captures the exact dependency tree so that `npm ci` can reproduce identical installs across environments.
Question 5: Which Node.js API would you use to schedule a function to execute repeatedly every 2 seconds?
- setTimeout(fn, 2000)
- setInterval(fn, 2000) (Correct answer)
- process.nextTick(fn)
- setImmediate(fn)
Correct answer: setInterval(fn, 2000)
`setInterval(fn, 2000)` executes `fn` repeatedly at approximately 2-second intervals until cleared with `clearInterval()`.
Question 6: What problem does the `async_hooks` module solve in Node.js?
- Making synchronous code run faster
- Tracking the lifecycle of asynchronous resources across async operations (Correct answer)
- Replacing callbacks with Promises automatically
- Monitoring memory usage per HTTP request
Correct answer: Tracking the lifecycle of asynchronous resources across async operations
`async_hooks` lets you track and propagate context through async call chains, useful for request tracing and diagnostics.
Question 7: In Node.js ES Modules, how do you import the `path` built-in module?
- const path = require('path')
- import path from 'path' (Correct answer)
- import { path } from 'node'
- load('path')
Correct answer: import path from 'path'
In ES Module syntax (`.mjs` or `"type": "module"`), you import built-in modules using the `import` statement.
Which environment variable is conventionally used to set the Node.js execution environment (e.g., development, production)?