Everyone gets hyped about how fast new JavaScript runtimes are, which indirectly (and sometimes directly) hints that Node.js is slow or lacks some integrated features. The pattern I see with Deno 2 and Bun is that they want to be the all-in-one toolkit for building JavaScript applications. They:
- automatically load your .env files
- run scripts in package.json
- install packages from npm
- come with a built-in API to write and run test suites
- and more
Using Node.js requires using third-party tools to achieve these tasks, such as using dotenv to load .env files.
However, Node.js has also been catching up with the competition. This article will focus on three of the many features that have been added since version 20 or 22. Let’s get to them.
Loading .env Files
A lot of us have relied on dotenv for loading environment files. We would write require('dotenv').config()
or import 'dotenv/config'
and thereafter access the value from process.env
. Starting from version 20.6.0, you can use the --env-file
flag to specify files from which Node.js should load environment variables.
As an example, the following command will load environment variables from the .env file:
node --env-file=.env index.js
You can also load multiple files by passing the flag twice:
node --env-file=.env --env-file=config.env index.js
Some people have reported that their app’s performance improved by removing the dotenv package and using Node.js directly. Here’s a tweet from Wes Bos about how his app’s startup time went from 1300ms to 1ms.
Watch Application Files
I’m sure you’ve used different tools to watch your source files and restart the application when you change your code. We’ve done this in various ways, one of which is using nodemon, a utility that monitors changes in your source files and automatically restarts the server.
Starting from version 18.11.0 and 16.19.0, the node CLI got two new flags for doing that, --watch
and --watch-path
. Running node --watch index.js
starts the application in watch mode, and will watch the entry point and any required or imported module. You should use the --watch-path
flag to specify the paths to watch, instead of the entry file and its imported modules.
There’s one more flag related to watch mode: the --watch-preserve-output
flag. You can use this flag if you want to prevent the console output from being cleared when watch mode restarts the process.
node --watch --watch-preserve-output test.js
Running Scripts in package.json
While Node.js doesn’t have a built-in package manager, most installations usually come with npm bundled in. You already npm run
or similar command from various package managers to run scripts defined in package.json. This isn’t a problem, but Bun and Deno bragging about the time it takes to start and run package.json scripts have likely made the Node team reconsider this option.
Starting with version 22.0.0, Node.js now has the means to run scripts using the —-run
flag, e.g., node --run dev
. This doesn’t just replace npm run
without any benefits and limitations. Using node --run
has been proven to be faster than npm run
command. Some features of other run
implementations that are intentionally excluded are:
- Running
pre
orpost
scripts in addition to the specified script. - Defining package manager-specific environment variables.
You can read more about this flag in the documentation.
Summary
This article explores recent improvements in Node.js that bring it closer to the integrated capabilities offered by Deno 2 and Bun. We examined features for:
- loading .env files using the –env-file flag starting from version 20.6.0
- native file-watching capabilities with the –watch and –watch-path flags introduced in versions 18.11.0 and 16.19.0
- a faster method for running package.json scripts with the –run flag available from version 22.0.0
These updates help Node.js catch up with other runtimes by reducing the need for third-party tools and improving performance, security and usability
Originally published by me on Telerik's Blog
Author Of article : Peter Mbanugo Read full article