Recent Updates in Node.js and Express You Need to Know
Updates in Node.js and Express include built-in testing tools and new environment variable options to streamline JavaScript projects and reduce dependencies.
Nov 21, 2024 • 3 Minute Read
Since its release in 2009, Node.js has been one of the most popular JavaScript runtimes due to its design, large developer community, and npm package registry. Over the past 10+ years, continual enhancements have been made in both Node.js and in many libraries and frameworks associated with it, like Express.js. In fact, the official Node.js schedule has new major releases coming every 6 months.
That means that even if you build and deploy Node.js and Express code, you may not always be aware of the latest APIs, features, or coding styles. Here are a few of the latest enhancements to Node.js and Express.
Environment variables
One recent example is how Node can work with environment variables. Starting with Node v20, you can pass the --env-file=<FILENAME> flag when running the node command. This defines the location of a text file that stores your environment variables. Every item defined in each line of that file is available from the process.env.ENV_VARIABLE_NAME value.
By using this flag, you don't need to load libraries like dotenv, and it's always a good thing whenever you can remove a third-party dependency.
Watch mode
Another recent enhancement in Node v22 is a watcher built directly into Node. Watchers allow you to reload your application when certain code files are changed, and they are very convenient during development. Without a watcher, every time you change a code file, you would need to stop and restart Node to see the change.
In v22, you can pass --watch along with a list of files, (or --watch-path and a directory) to turn on this feature without needing to import third-party watchers like nodemon.
Testing
Starting in Node v20, Node’s built-in test runner reached a stable release. This means that instead of pulling in libraries like Mocha or Jest, you can import built-in tools with node:test and node:assert.
Testing is an important process where you document how an application is supposed to work by writing lots of small checks with code, called tests, and regularly run those tests. If they fail, you know that something isn’t working correctly before you accidentally deploy a broken application.
Functionally, there’s not a big difference between using built-in tools and third-party dependencies, but like I mentioned with environment variables, including less third-party imports helps keep your code more secure and easier to maintain long-term.
Replacing the body-parser library
For years, the body-parser library was a common way to parse Express request body data. Clients would send a POST request with some body data in formats like JSON or URL-encoded form data, and functions included in the body-parser library helped you parse that in the routes that handled those requests.
Starting in Express 4.16, the main functionality of body-parser was included in the built-in json and urlencoded middlewares, which means you don't need to import body-parser any more to take advantage of them.
Express 4.16 was released way back in 2017, and even though it’s better to use express.json() and/or express.urlencoded() to parse JSON and form request bodies for most use cases, there are still a lot of use cases and sample code that use body-parser.
Stay current on Node.js and Express with Pluralsight
Node.js is always changing; even if you have experience using it with frameworks like Express, it's always worth refreshing your understanding with the latest versions.
Pluralsight's Node.js path courses target Node v22.7.0 and later, and Express 4.19 and later. They're also designed to be as short and to the point as possible to get you up and running quickly whether you're brand new to Node.js or a seasoned Node.js developer just catching up with the latest techniques.