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)
- Head over to the Node.js download page and download the latest LTS version for your operating system (OS). On Mac OS X, you can also use brew to install Node.js using this command
brew install node
- Alternatively, use a Node version manager to install Node.js. If you are on Windows have a look at nvm-windows. For Mac OS X, check out nvm. For Linux distributions, check out NodeSource. For a more complete list of options, check out the npm.js "download and install Node.js" documentation page
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
- LTS stands for Long Term Support, and this version is recommended for production deployments. A LTS version is considered stable, meaning it will not receive the latest features from the current version, but on the other hand it also means you can do updates without the fear of installing a version containing breaking changes. After an LTS version got release any critical bugs will be fixed for a total of 30 months
- The current version is more modern, but some newer features are likely to change, so if you want to try out the latest features before a Node.js version moves to the LTS phase, then the current version is the one you should download
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.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:
And this is the content of your config.env you have:
Then, in your code, you can use the environment variable like this: