Node.js 10.0 Fills Some Gaps, Polishes a Few Rough Edges
Node.js has quickly risen to become one of the most popular backend development frameworks. And the latest update has only made it better!
Join the DZone community and get the full member experience.Join For Free
node.js 10 was released this april and i just wanted to kind of touch on what's new—the additions improvements, etc. obviously, i'm not going go through the extensive change log or anything but i just want to mention the major things. the changes are so many that you’ll find this information in series of articles elsewhere but i figured that i would wrap it up in one article and try to convey it in a simple way.
standardized error codes
if you’ve worked with errors and debugging node, you know it can be kind of a pain. before we got some weird string-based error messages that we'd have to constantly look up or sometimes guess what they mean, but now with node.js 10 they adopted standardized error codes. we have consistent codes with a repeatable pattern instead of a string with no identifiers.
this will make debugging and error handling easier and it will remove a lot of the guessing and the searching—looking up things on google and stack overflow. applications are easier to debug and offer faster to develope.
with node.js 10, we can actually check for error codes. we'll get a specific error code that we can handle. node.js 10 is will make debugging easier.
http/2 is a higher performing successor to http. if you really haven't looked into http/2, it’s not a rewrite of the protocol or anything like that. all the http methods, the status codes, the semantics are the same. you should be able to use the apis as with http/1. x. there will be some additions but everything should be backward compatible.
http/2 was introduced in node 8 but it was known to be very buggy. in version 10, it's now completely stable and safe to use. as far as frameworks go, the hapi.js framework and koa framework fully support http/2 right out of the box. as far as express is concerned, you need to use the express-http/2-workaround package. the focus of http/2 is performance, specifically end-user perceived latency and network and server resource usage.
having stable http/2 is a pretty nice addition to node.js 10. let’s take a look how the code would look with hapi and http/2.
as you can see, we’re basically creating a connection or setting our routes and handlers. the syntax is not much different. we’re just using the http/2 module.
better support for esm modules
node.js 10 does offer better support for esm or ecmascript modules. for years, node has used commonjs modules.
if you don't know what that is, it's when we bring in our packages using const, equalrequire, express. etc. ecmascript modules, or es6 modules, are where we import something from somewhere else. for example react, angular, and most front-end frameworks use syntaxes with webpack and babel.
understand that pure native ecmascript modules are not standardized yet. they're not ready to be used by default in node.js 10. they are getting closer and it's a huge priority. it’s difficult for them to integrate along with the commonjs modules.
for now, we can enable the experimental-modules flag and any file where you want to use esm or ecmascript modules. you just have to give it .mjs extension. i'll be happy when we can just use es6 modules right out of the box.
every time there was a node update, developers would have to completely revise a lot of code due to its binding to v8. there's higher level of abstractions that can make this much easier for them.
with node.js 10, open ssl has been updated to version 1. 1. with tls 1. 3, we'll have faster ssl handshakes and even better security. node.js 10 includes the chacha20 cipher, which is a high speed cipher. it is up to three times faster than aes, which is the current industry standard. there is also a poly1305 authenticator for authenticated encryption, which is going tomake node more secure overall.
if you've worked with node you've probably worked with the fs, or the file system, module which allows you to interact with the file system. in node 10, we have the experimental fs.promises, which means we can now use asynchronous file system methods that return a promise. we can use:
. we'll get methods where we don't need to use callbacks and it will make our code cleaner and readable if you use async/await.
with fs.promises in node 10 for error handling, we don't need to use try-catch blocks because where it's promise based we can now use
. let’s look at an example.
in the above code, we have the regular fs or filesystem module and then the promises-based one. we're just reading a file synchronously and we have the
method that is using the
module along with async/await to truncate this.
the code is truncating it into four characters and then we're just logging it out. it started off as node.js. now it's going to just read the
method, which is it much more elegant than using something like callbacks, where we're using async/await. as in the last line of the code example, we can use
. if we have any errors, we can log those errors.
node.js is getting faster.
node.js 10 is shipped with npm 6 which comes with its own set of improvements including performance, security, and stability. i did underline security because that seems to be like the main theme of npm 6. npm recently acquired lift security and the node security platform which identifies vulnerabilities for developers and security vendors.
with node and npm 6, you should start to get automatic warnings when trying to use code and packages with known security issues. we have the new audit command npm audit. the command will look at your applications dependency trees, identify if anything is insecure, tell you about it, and then if you wanted to, you could switch it out for an alternate, safer dependency.
npm is now 17 times than it was exactly one year ago, which is pretty impressive progress.
Published at DZone with permission of Aditya Modi. See the original article here.
Opinions expressed by DZone contributors are their own.