Node.js Interview Questions for Freshers
12 Node.js interview questions for freshers and students (0 to 1 year): npm scripts, require and modules, process and paths, the fs module and a basic HTTP server. Each answer explains why the other options are wrong.
CoversEsmFsGlobalsHttpModulesNode.jsNpmPathProcess
By Deepak KumarPublished Answers checked by Deepak
Take it as a timed test
- 12 questions
- 15 min
- Pass mark 60%
- Answers shown at the end
No sign-up. Your answers and scores stay in this browser.
This freshers paper has 12 Node.js questions for freshers and students with roughly 0 to 1 year of experience. It covers npm scripts, require and modules, process and paths, the fs module and a basic HTTP server. Ten questions are on this page with full explanations; the timed test also includes two questions that aren't shown here, so practise for understanding rather than memory.
How should you use this paper?
Answer each practice question before opening its explanation, and say your reasoning out loud the way you would in an interview. Then take the timed test: 15 minutes, a pass mark of 60%, with answers shown at the end. Your report breaks your score down by topic and difficulty, and you can retry only the questions you missed.
Practice questions with answers
Try each one before opening the answer. The explanation covers why the other options are wrong. The timed test also uses 2 questions that aren't shown here, so practise for understanding, not memory.
What is the scripts section of package.json for?
Show answer and explanation
Answer: A. Named commands you run with npm run <name>, such as dev or test
scripts maps short names to shell commands, so the team runs npm run dev instead of remembering long commands. npm adds node_modules/.bin to the path, so locally installed tools like jest or tsc work without a global install. start and test can be run without run. Dependencies live in dependencies, not here.
In a CommonJS module, what does require('./config') give you?
Show answer and explanation
Answer: A. Whatever ./config assigned to module.exports
require runs the module once, caches it, and returns its module.exports object. Anything not attached to module.exports stays private to the file. A common bug is writing exports = { ... }, which only reassigns the local shortcut and breaks the link; use module.exports = { ... } or add properties with exports.name = ....
__dirname is available in ES modules (.mjs files) the same way as in CommonJS.
TrueFalse
Show answer and explanation
Answer: False
False. __dirname, __filename and require are CommonJS module wrappers and don't exist in ES modules. In ESM use import.meta.dirname and import.meta.filename (available in current Node.js releases), or derive them from import.meta.url with fileURLToPath. This trips people up when they switch a project to "type": "module".
You run node app.js hello. What is in process.argv[2]?
Show answer and explanation
Answer: A. "hello"
process.argv is an array of strings: index 0 is the path to the Node.js executable, index 1 is the script path, and your own arguments start at index 2. So process.argv[2] is "hello". For real command-line tools, Node's built-in util.parseArgs handles flags and options so you don't parse the array by hand.
What does path.join('/a', 'b', '../c') return?
const path = require('path');
console.log(path.join('/a', 'b', '../c'));Show answer and explanation
Answer: A. /a/c
path.join joins the segments with the platform's separator and then normalises the result, resolving .. and .. So /a/b/../c becomes /a/c. Build file paths with path.join or path.resolve rather than string concatenation, which breaks on Windows and leaves double slashes behind.
Which built-in module do you use to create a basic web server without any framework?
Show answer and explanation
Answer: A. node:http
http.createServer((req, res) => { ... }) gives you a request handler, and server.listen(3000) starts it. Frameworks like Express are built on top of it. net is lower-level raw TCP, fs is for files, and url parses addresses. The node: prefix makes it clear you mean the built-in module, not an npm package with the same name.
Which of these can you use in a CommonJS Node.js file without importing anything? Select all that apply.
Show answer and explanation
Answer: A. process + B. Buffer + C. require
Node.js provides process, Buffer, timers, console and, in CommonJS, require and module. window and document are browser APIs for pages and don't exist in Node.js; the global object is globalThis. Code shared between browser and server should check for these before using them.
What is the difference between dependencies and devDependencies?
Show answer and explanation
Answer: A. dependencies are needed to run the app; devDependencies only for building and testing it
Runtime packages such as Express go in dependencies. Tools you only need while developing, such as test runners, linters and TypeScript, go in devDependencies. A production install with npm ci --omit=dev skips the dev ones, giving smaller images and fewer packages that could carry vulnerabilities.
Match each fs call to what it does.
- fs.promises.readFile
- fs.promises.writeFile
- fs.existsSync
- fs.promises.mkdir with recursive: true
- Checks if a path exists, blocking briefly
- Reads a whole file without blocking
- Creates a folder and any missing parents
- Creates or replaces a file with new content
Show answer and explanation
Answer: fs.promises.readFile → Reads a whole file without blockingfs.promises.writeFile → Creates or replaces a file with new contentfs.existsSync → Checks if a path exists, blocking brieflyfs.promises.mkdir with recursive: true → Creates a folder and any missing parents
The promise versions don't block the event loop, so prefer them in servers. writeFile overwrites by default; use appendFile to add. existsSync is fine in startup code, but checking then reading creates a race, so it's usually better to just try the operation and handle ENOENT. recursive: true also stops mkdir failing if the folder exists.
What does npx some-tool do?
Show answer and explanation
Answer: A. Runs a package's command, using the local copy or downloading it temporarily
npx runs a package binary. If the project has it in node_modules, that copy runs; otherwise npx can fetch it temporarily, which is handy for one-off generators. It avoids global installs, which drift between machines. Be careful running unfamiliar package names with npx: a typo can execute someone else's code.
Answered them all? Take the same questions against the clock and get a topic-by-topic report.
Start the timed test