Nvm (Node Version Manager) allows you to quickly install and use different versions of node via the command line. Install nvm-windows. Managing node versions Check your current node version node -v Install specific node version nvm install 16 Install the latest lts version nvm install lts Switching to specific Node version This command needs admin […]

This article will demonstrate how to test JavaScript files in NodeJS using the libraries Sinon, Mocha and Chai. Sinon is often installed together with Mocha and Chai, but it is not required.

Slow I/O operations such as accessing disk and network resources are handled via Node’s Event Loop. The Event Loop is started once you run a node script and exits once there are no more callbacks to process. Heap and Stack V8 is node’s runtime engine and mainly consists of a Heap and a Stack. The […]

V8 and its feature groups Node’s build-in JavaScript engine is V8, but others such as Chakra (by Microsoft) can be used. V8 has three feature groups: Shipping (stable features, enabled by default) Staged (almost stable, needs –harmony flag to be enabled) in Progress (experimental, also needs specific flags to be enabled) See all options via […]

require() vs import In the REPL all node modules are already present so you do not have to explicitly require() them first. But when you write your own files you must choose between one of two options: or use ES6-style with .mjs file extension. How modules are resolved There are a number of steps involved […]

There are a couple of ways to execute files and commands, but all use the module child_process. child_process.exec creates a shell and also buffers the whole command’s generated output and sends it to a callback function. So use it only if the output is not too big child_process.execFile runs a file without a shell but […]

Setting and reading custom env You can define custom env variables in two ways: Option 1: In one line before running node: Option 2: By exporting them one by one: After that you can read the env variables in your script: By the way, process needs no import/require. OS user environment variables process.env contains a […]

Streams are collections of data that might not be available all at once and don’t have to fit in memory all at once. For example if you want to serve a file that is 1GB over HTTP you would create a stream instead of reading the file using readFileSync. A readable stream can be piped directly to another writable stream.

Sync vs async operations Node offers both, sync and async methods to handle files. Sync methods (having sync in the method name, e.g. fs.readFileSync()) will block the execution of the program until it has finished processing, so it is recommend to either always use async operations or at least for heavy operations, such as handling […]

There are many strategies to scale your application: Cloning Decomposing Splitting The cluster module helps you to scale your code on one system that has many CPU cores. The idea is: You have one master process and clone your code by forking as many worker processes as you have CPU cores in your system. This […]

What you can achieve are things like: Creating an HTTP server We create an HTTP server on port 8080 and respond to any request with a ‘Hello there!’: IncomingMessage extends two interfaces EventEmitter and ReadableStream which allow us to listen to events and process streams, such as incoming data. Creating an HTTPS server HTTPS has […]