Chris.luChris.lu header image, displaying an 80s style landscape and sunset

Node.js

a waterfall of code with the text node.js

When Node.js first showed up, most people were probably using PHP, Java or Ruby as backend language. Node.js gave us the possibility to use Javascript outside a web browser, meaning that for the first time we could use the same language for backend as well as frontend code. If you add other tools like Electron, Cordova or Capacitor to your stack and you can use Javascript for your desktop and mobile apps too. It will never be possible to have a fully Isomorphic cross platform codebase, but by using Node.js you can reuse some of your Javascript code.

Node.js is an open-source, cross-platform JavaScript runtime environment that executes JavaScript

Under the hood, Node.js uses the Chrome (chromium) V8 engine, meaning it is based on the same asynchronous, event-driven model that Chrome uses. Still, there are differences. If you would like to learn more about those differences, check out the Node.js article Differences between Node.js and the Browser.

The significant advantage when using Node.js is that web applications can be written using a single programming language rather than different languages for server-side and client-side scripts. If later you also use other tools like capacitor or electron, then you will be able to go a step further and create mobile and desktop apps for your project and still use Javascript as the only programming language, which also means that a lot of the code you wrote can be shared between the backend, frontend and even your apps.

Installation

There are several options when it comes to installing Node.js. The first option is to use an installer for a specific version of Node.js, which is a good solution if you install Node.js on a server. The second option uses a Node version manager to install Node.js, which is usually a better solution for your local machine as it has the advantage that you can install several versions of Node.js in parallel on your computer. Using a Node version manager, depending on which project you work on, you can switch between versions (for example, you might want to use the latest version of Node.js for a personal project and have another LTS version of Node.js installed in parallel for a work-related project)

Should I install the LTS or the current version

It depends 😉

If you go to the Node.js download page, you will see that Node.js different versions: some are labeled as LTS and others are labeled Current

Checking the current Node.js version

You can use the following command in the VSCode terminal (or your favorite command line tool) to check which version of Node.js is currently installed:

node -v

Checking if your current Node.js is still secure

Sometimes you wonder if you need to update Node.js on your server(s) to fix a security flaw or if you can continue to use the current version (for another ten years 😉)

The Node.js team member has released a little tool called is-my-node-vulnerable, all you need to do is run this command:

npx is-my-node-vulnerable

To learn more about what it does and especially how it does it, I recommend checking out the projects README, to give you an idea of what it does I will quote a few lines from their README:

is-my-node-vulnerable helps ensure the security of your Node.js installation by checking for known vulnerabilities.

It compares the version of Node.js you have installed (process.version) to the Node.js Security Database and alerts you if a vulnerability is found.

Node.js alternatives

If, for some reason, you are unhappy with Node.js or are just curious and want to know more about alternatives, then I recommend checking out the following two JavaScript runtimes:

Deno is a JavaScript runtime that prioritizes productivity, security, and performance. Key features include built-in TypeScript, JSX, formatter, linter, and testing tools. Deno also supports creating standalone executables.

Bun is an all-in-one JavaScript toolkit and runtime designed for speed. It includes a bundler, a test runner, and a Node.js-compatible package manager.

Did you know?

Node.js now has built-in .env file support

One of the first dependencies developers usually add to their Node.js project is dotenv, but since v20.6.0 Node.js has built-in .env file support, so you might not need an extra package anymore

If you use the following command:

node --env-file=config.env index.js

And this is the content of your config.env you have:

FOO='bar'

Then, in your code, you can use the environment variable like this:

let foo = process.env.FOO