Interview Q&A / Node.js

INTERVIEW Q&A · 68 QUESTIONS

Node.js interview questions

The event loop on the server, streams, modules and building APIs with Express.

  1. What is the difference between dependencies and devDependencies, and why do we need package-lock.json?

    dependencies are packages your app needs to run in production; devDependencies are only needed while developing, testing or building. package-lock.json records the exact version of every installed package so every machine and deployment gets the same dependency tree.

    BeginnerIn-depth3 min read
  2. How do you tell Node.js to treat your files as ES modules?

    Set "type": "module" in package.json so .js files are ESM, or use the .mjs extension for individual files. Use .cjs to force CommonJS inside an ESM project.

    BeginnerQuick answer1 min read
  3. What are npm scripts and how do you run them?

    Scripts are named shell commands in the scripts field of package.json. Run them with npm run name; start and test also work as npm start and npm test. Local binaries in node_modules/.bin are on the PATH automatically.

    BeginnerQuick answer1 min read
  4. What do the caret (^) and tilde (~) mean in package.json versions?

    Caret allows updates that do not change the leftmost non-zero version number, so ^1.4.2 accepts 1.x.x at or above 1.4.2. Tilde allows only patch updates, so ~1.4.2 accepts 1.4.x at or above 1.4.2.

    BeginnerQuick answer1 min read
  5. What does the main field in package.json do?

    main names the entry file that Node loads when someone requires or imports your package by name. If it is missing, Node falls back to index.js. Modern packages may also use the exports field.

    BeginnerQuick answer1 min read
  6. What are peerDependencies in package.json?

    peerDependencies declare packages your library needs but expects the host application to install, so only one shared copy exists. Plugins and UI libraries use them, for example a React component library requiring react.

    IntermediateQuick answer1 min read