1) How to stop/exit from Node.js application?

Use global object process.exit method - process.exit(1). However it's no more recommended practice https://nodejs.org/api/process.html#process_process_exit_code, because process.exit() will close application despite some events exist in event loop. To exit from Node.js gracefully use following syntax
process.exitCode = 1;
To stop execution from terminal use ctrl - c combination.

2) How to set environment variables ?

Environment variables can be specified before starting application, using following syntax:
NODE_ENV=production PORT=3000 $ [runtime] [program_name]

Finally, get environment variables from process.env

3) How to use async/await in node.js ?

Since 7.6.0 Node version async/await is available by default. In older version you need to specify –harmony-async-await option. If you are using typescript, set "target" property to "es6" in tsconfig.json and your code will automatically transpiled.

4) How to get query string variables in Express.js request handler ?

Query string stored in req.query object in key/value pairs. Also it's possible to extract query params from any url using standard node package "url".
import * as url from "url";
const queryParams = url.parse("https://stackoverflow.com/search?query=nodejs")
console.log(queryParams.query) // nodejs

5) How to check Node.js version installed ?
Using -v or -version command line argument.
$ node -v

6) How to close close socket connection in "net" module?

Method net.createConnection() returns a Socket object which has destroy(), which handle correct socket closing.

7) Write simple web-server which serve html files.
import * as connect from 'connect';
import * as serveStatic from 'serve-static';
connect().use(serveStatic(__dirname)).listen(process.argv[3] ? process.argv[3] : 3000, function() {
    console.log(`Server running on ${process.argv[3]?process.argv[3]:3000}`);

8) Why using blocking/sync operations is bad practise in Node.js?

Node.js is single threaded, therefore one sync operation can block event loop, and all other will be stopped. Imagine you serving http requests, you received file read request & start reading this file - until you file read operation complete you can't handle no more request. Such practise may work in development, but not in production. Fortunately, node provide both synchronous and asynchronous API, for example : readFile and readFileSync.

9) What is Continuation-passing style(CPS)? Why it is so popular in Node.js?

CPS is a style when function don't return result of execution directly, it return result to specified callback function. As you already know, asynchronous programming is key principle in JS, so naturally it widely used in Node. For example, for IO operations like file reading/writing. But CPS has one serious problem, it's very easy lead to callback hell problem.

10) How to created a module?

At first create some file 'lib.js', then add export expression, that's all. If you are write in TypeScript use import/export.
var exports=module.exports={};
exports.someFunc=function someFunc() {
  console.log(`Node is awesome!`)
// another file
const utils=require('./lib.js');

11) What is the difference between setTimeout vs process.nexttick & setImmediate?

setImmediate() push the callback ahead of the job queue, so it will be executed before setTimeout(fn,0). process.nextTick() will be processed after the current operation completes, regardless of the current phase of the event loop.
// will print nextTick setTimeout setImmediate

12) What is a cluster module? How it helps scaling Node apps?

Cluster module is built-in module which provides utilities for forking your single thread app and fully utilize multiple core system power. Using this module unlimited number of child processes can be created, moreover IPC communication is available.

13) How to manage project dependencies?

Project dependencies are managed via package.json file. There is two main package managers: NPM and Yarn. To add some dependency just run command npm install "moment", after this you can import this utility in your code. Also in package.json file you can specify development dependencies, to add as little overhead in production as possible, for this add --save-dev option when installing module.

14) How to copy file in Node.js?
 The most elegant way, is to use createReadStream & createWriteStream methods, or utilize 'fs-extra' package, which provides many useful functions, not available in built-in 'fs' module
const fs = require('fs');
const fs = require('fs-extra')

fs.copy('dbDump.zip', 'dbDumpCopy.zip')
  .then(() => console.log('File was successfully copied!'))
  .catch(err =>console.error)

15) How to enable gzip compression for HTTP server ?

If you are using Express.js framework, there is "compression" middleware available.
import * as compression  from 'compression'
import * as express from 'express';
const app = express();
function shouldCompress (req, res) {
  if (req.headers['x-no-compression']) {
    return false
  return compression.filter(req, res)
app.use(compression({filter: shouldCompress}))

16) How to hash string with md5 function?
 Using built-in module "crypto".
let message = "I love node and express.js";
const crypto = require('crypto');
let hashed=crypto.createHash('md5').update(message).digest("hex");

17) Write functions which loads image from URL?
import * as fs from 'fs';
import * as request from'request';

function downloadImage(uri, filename){
  return new Promise((resolve,reject)=>{
    request.head(uri, function(err, result, body){
        return reject(err);
      request(uri).pipe(fs.createWriteStream(filename)).on('close',()=> resolve());

downloadImage('https://docs.npmjs.com/images/npm.svg', 'npm-logo.svg').then(()=>console.log('Images saved.')).catch(er=>console.error);

18) What is stream in Node.js?

Stream - is a concept first introduced in UNIX to pass data from one program to another for I/O operations. This feature helps make programs more independent & specific, consequently chaining several programs results become easier. Another great feature that stream allow to process big files, which doesn't fit in RAM. To implement such functionality in Node.js use 'stream' module. There is 4 types of streams: readable,writable,duplex,transform. Streams can manipulate with Buffer or string data. Example of writable streams are: HTTP requests,responses,TCP sockets.

19) How to do Base64 encoding/decoding ?

Using buffer toString method, any data can be encoded: image,text, binary data.
let base64Data=Buffer.from("I love JS!").toString('base64'); //SSBsb3ZlIEpTIQ==
let decodedData=Buffer.from(base64Data, 'base64').toString('utf8'); // I love JS!

20) How to run cron jobs in Node.js?

To run simple cron job setInterval can be used, for more complex intervals external lib like 'node-cron' should be used, it supports GNU crontab like syntax.
import * as cron from 'node-cron';
cron.schedule('5 8 * * Sun', function(){
  console.log('Running every sunday');

21) How to automatically restart a node server on crash or system reboot?

To automatically you node application on system start use Linux 'upstart' utility. To restart application oun crash use forever or pm2 CLI tool.