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

Node.js

a waterfall of code with the text node.js

Node.js is an open-source, cross-platform JavaScript runtime environment that executes JavaScript code outside a web browser, allowing developers to use JavaScript for server-side scripting.

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 good for beginners experimenting with Node.js and wanting a quick and easy installation. The second option uses a Node version manager to install Node.js. This has the advantage that you can install several versions of Node.js in parallel on your computer, and then, 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

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