What does `module.exports` vs `exports` mean when you write only `exports = { ready: true }` in a module?
// flags.js
exports = { ready: true };
// app.js
console.log(require('./flags'));Read the full question →Node puts JavaScript on the server with a single-threaded event loop and a large streaming and process API surface.
Event-loop phases, the difference between setImmediate and process.nextTick, backpressure on streams, cluster and worker threads, and how to avoid blocking the loop.
Where it shows up: API services, BFF layers, build tooling, and serverless functions across most JavaScript-first companies.
Twenty real NodeJS questions from the library, code snippet included. Nothing here is paraphrased for search engines. It is the same text a signed-in user sees.
// flags.js
exports = { ready: true };
// app.js
console.log(require('./flags'));Read the full question →// index.js (no "type": "module")
import os from 'os';
console.log(42);Read the full question →Promise.resolve().then(() => {
console.log('then');
process.nextTick(() => console.log('nextTick'));
queueMicrotask(() => console.log('microtask'));
Promise.resolve().then(() => console.log('inner then'));
});Read the full question →const fs = require('fs');
try {
fs.readFile('nope.txt', (err, data) => {
if (err) throw err;
console.log(data);
});
} catch (e) {
console.log('caught:', e.message);
}Read the full question →const w = new Writable({
write(c, e, cb) { cb(); },
writev(chunks, cb) { console.log('writev', chunks.length); cb(); }
});
w.cork();
w.write('a'); w.write('b'); w.write('c');
process.nextTick(() => w.uncork());Read the full question →async function run() {
const r = await [Promise.resolve(1), Promise.resolve(2)][0];
console.log(r);
}
run();Read the full question →console.log(process.env.DEBUG);Read the full question →const fs = require('fs');
fs.readFile(__filename, 'utf8', (err) => {
if (err) throw err;
console.log('done');
console.log(data);
});Read the full question →const { isMainThread } = require('worker_threads');
console.log(isMainThread);
// run as: node app.jsRead the full question →const fs = require('fs');
fs.readFile('config.json', 'utf8', (err, data) => {
console.log(data.length);
if (err) console.log(err);
});Read the full question →const { Transform } = require('stream');
const t = new Transform({
readableHighWaterMark: 5,
writableHighWaterMark: 10,
transform(c, e, cb) { cb(); }
});
console.log(t.readableHighWaterMark, t.writableHighWaterMark);Read the full question →const cp = require('child_process');
const r = cp.spawnSync('echo', ['$HOME', '&&', 'whoami']);
console.log(JSON.stringify(r.stdout.toString()));Read the full question →// .env contains: PORT=3000
const port = process.env.PORT;
console.log(typeof port); // ?
const server = app.listen(port);Read the full question →const x = Buffer.from([1, 2, 3]);
const y = Buffer.from([4, 5, 6]);
const z = Buffer.concat([x, y], 4);
console.log(z.length, [...z]);Read the full question →// file: /app/src/server.js
const path = require('path');
console.log(path.join(__dirname, '..', 'config.json'));Read the full question →exports = { a: 1 };
module.exports.b = 2;Read the full question →const fs = require('fs');
fs.readFile(__filename, () => {
setTimeout(() => console.log('timeout'), 0);
setImmediate(() => console.log('immediate'));
});Read the full question →let shuttingDown = false;
process.on('SIGTERM', async () => {
await drainQueue();
await server.close();
process.exit(0);
});Read the full question →const emitter = new EventEmitter();
function handle(req, res) {
emitter.on('tick', () => res.end('ok')); // per request
}Read the full question →for (const url of urls) {
const res = await fetch(url);
console.log(res.status);
}Read the full question →We keep the correct answer, the explanation and the scoring behind sign-in so the platform stays honest, which is why the answer options and the worked explanation for every NodeJS question stay behind a free account. Signing in is free and takes a few seconds.
48 distinct NodeJS topics are tagged across the library. These are the real topic labels stored on the questions, not a hand-written list.
…