Node.js Fundamentals

Node.js Fundamentals

Launch Your Journey with Setup and Tips

Welcome to the first installment of our Node.js Fundamentals series! As I gear up for the JSNAD certification, I'll be sharing essential concepts to boost your Node.js prowess. This article covers three exciting topics to help you set up NodeJS on your machine – so join the fun and follow along! :)

The article has three objectives:

  1. Walk you through setting up node.js on your pc

  2. Talk about Node.js Binary

  3. Walk you through Debugging and Diagnosing Javascript in Node.js.

Curious about Node.js? It's a game-changing technology that enables developers to run JavaScript instructions on their machines without needing a browser. Check out this wiki for more info, and don't miss the insightful documentation! Before the dawn of Node.js, JavaScript was limited to browser-based execution, adding functionality to web pages. But with Node.js, server-side logic written in JavaScript became a reality, empowering front-end developers to grasp and even contribute to the back-end magic with ease! 😄

Setting up

In the intro, we mentioned how Node.js enables JavaScript execution on your personal computer and various platforms. In this article, we'll focus on setting up Node.js in three primary environments: macOS, Linux, and Windows. We'll utilize a version manager for installation – while alternative methods exist, using a version manager streamlines the management of Node.js binary versions and ensures consistency in file and folder placement, preventing compatibility issues.

Setup on macOS and Linux

We are using Node Version Manager (nvm) the current nvm version as of October 2022 is v0.39.2. We will be installing nvm via this install script. You can edit the link to replace the version with the latest one if there is an update when following along.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

You can replace bash with zsh (on newer macOS releases).

Run the below Command to confirm the nvm installation was successful; you should see nvm as an output

command -v nvm

If the above command fails on Linux, close and reopen the terminal(or SSH session) and re-run the command. For in-depth troubleshooting on mac, follow this link.

I’d be using the node 16 stable version for the examples in this series, so run the below command to install it.

nvm install 16

After the above command runs successfully, you need to activate this newly installed version by running the below command

nvm use 16

Setup on windows

For Windows installation, we'll be using a different Node version manager. While I won't be able to provide screenshots as I did in the Mac installation guide, I'll strive to be as descriptive as possible to ensure a smooth and comprehensible process

In this section, we will be looking at installing NodeJs on windows 10 and higher, As a non-windows user, you can skip this portion of the article. Nvm is the popular version manager on macOS and Linux, but on windows, we’d be using nvs. The nvs version manager is cross-platform, but not as popularly used on macOS and Linux. Goto, this link to download the MSI installer file of the latest release. After downloading, run the installation wizard, then open your cmd.exe or PowerShell prompt to use nvs to install a NodeJs version 16.

nvs add 16

After the above command runs successfully, you need to activate this newly installed version by running the below command

nvs use 16

You can use the nvm to install several versions of node.js and switch between them as needed.

Confirm that NodeJS has been installed successfully

Regardless of the installation guide you followed above, we’d need to establish that we have installed node version 16 using a node version manager, and also that we have the correct npm version installed alongside.

Run the below commands, you should similar output

node -v
output >> v16.13.2

npm -v
output >> 8.1.2

NodeBinary

The node binary executable is almost the representation of the entire node.js platform. We would explore some of the command line flags offered by this binary.

Printing Command options

Below is an example to view all Node command line flags

node —-help

To check additional flags for modifying the javascript runtime engine: v8 run the below command

node --v8-options

Checking syntax

Suppose you've just completed writing a script and executing it would incur some costs. To ensure it's free of syntax errors before running, follow the example below, assuming your script is stored in a file named app.js:

node --check app.js 

Dynamix evaluation

Looking for a swift way to test a code snippet or run an operation platform-independent? You can do so using either the -p | --print or -e | --exec flags to achieve this effortlessly.

PS: try this
node --print “1 + 1”
output >> 2

PS: run in a folder that has js files in it
node -p "fs.readdirSync('.').filter((f) => /.js$/.test(f))"

You can run straightforward tasks like the initial example or even platform-independent operations, such as the final statement that displays all .js files in the current working directory.

You can also preload a CommonJS module using the -r | --require flag before loading anything else. This comes in handy when your script relies on a module that plays a crucial role in the process or when you need to configure a script, such as with the dotenv module.

Assuming we have a preload.js with the content below:

./preload.js

Then with another app.js file with the below content:

./app.js

To preload the preload.js before running the main file app.js assuming the two files are in the same folders, you’d need to the command depicted below

node -r ./preload app.js

Stack trace Limit

While trying to debug an error, you want to see more of the function calls as the default is not showing any of your code, instead, you are seeing only third-party functions, just to be sure of where this particular bug is being triggered from, well this default call stack trace can be adjusted simply by using the following commands

node --stack-trace-limit=<new limit | defaults to = 10>

Debugging and Diagnosis

To put a Node.js process in a debuggable state, you need to run the process in an inspect mode:

node --inspect script.js
node --inpect-brk script.js

The --inspect-brk adds a breakpoint at the beginning of the program, so as to not perform full initialization of the process that might run some asynchronous code.

The inspect mode exposes the process via a remote protocol that can be connected to a debugger like Chrome devtool. This inspect mode also opens room for other diagnostic checks on the node process.

Breakpoints

allow you to pause process execution, giving you the opportunity to inspect the logic, which may include checking variable contents, viewing call stacks, and more. To demonstrate, we'll walk through an example below.

First, copy the content provided into an app.js file:

./app.js

After that run the below command

node --inspect-brk app.js

You’d need to begin debugging the process by opening your chrome browser tab and pasting the below address into it

chrome://inspect

You would be presented with the screen below, click on the inspect link circled in the picture below to open an instance on your chrome’s Devtool and you’d be connected to the Node process

Because we used the --inspect-brk instead of the --inspect, the process pauses on the first function call, this is depicted in the screenshot below

If you examine the circled area in the screenshot above, you'll notice a checkbox labeled 'Pause on caught exception.' Selecting this option instructs the debugger to pause on Error. Additionally, you can choose specific points to pause by adding the 'debugger' keyword on the desired line within the panel containing your source code.

The icons on the panel are extremely helpful for examining process execution, while other tabs like 'scope' allow you to view variable values within a specific scope of the process, among other features. If you're interested in becoming proficient with these tabs and icons, check out this YouTube video that delves into Chrome DevTools in greater detail.

As we wrap up the first installment of our Node.js Fundamentals series, you're now primed and ready for the exciting journey ahead! In the upcoming parts of this series, we'll dive deeper into the world of Node.js, uncovering its vast potential and showcasing how it can revolutionize the way you perform platform-independent automated tasks. Stay tuned for more insights, tips, and techniques that will empower you to unlock the full power of Node.js!