Node 10 was released last year and it’s going to be enter the LTS phase this October. So it is about time to fresh up on some of the Node 10 features and start rolling out to production environments if you haven’t done so already. I am going to list the most important ones down below with details and also some examples:
- Error handling
- N-api official support
- HTTP/2 support
- V8 engine performance boost
- ESM modules
- Experimental fs native promises
- New console method
It’s no secret that node has’t been particularly great with error handling and more specifically error objects. You could’ve checked on the message but that’s clumsy and far from ideal. Most folks out there ended up writing their own libraries to handle errors in a more elegant manner. That’s no longer the case as Error got some attention in Node 10.
There are now a couple of error types: ReferenceError, SyntaxError, RangeError while the Error object has been extended and supports now the following properties:
code<string> The string error code
errno<number> The system-provided error number
message<string> A system-provided human readable description of the error
syscall<string> The name of the system call that triggered the error
path<Buffer> When reporting a file system error, the
pathwill identify the file path.
dest<Buffer> When reporting a file system error, the
destwill identify the file path destination (if any).
Probably out of this the greatest addition is the error code which is now a part of a big list of predefined error codes which can be checked upon. The full list here.
N-api full support
I think this is still in the beginning and although there is some documentation, there are not that many examples so I wrote a few and put them here.
As a modern development ecosystem Node was due for this, as the standard was approved May 2015. A summary of benefits that HTTP/2 brings to the internet can be found here.
V8 engine upgrade
ESM experimental modules support
With ES6 introduction in 2015, a new way of importing and exporting modules has been introduced and Node is struggling implementing it ever since. I am talking about this import/export vs require and module.exports. It’s still ongoing and you can try it out using the experimental flag on node but to me the integration is still ugly at this point.
Experimental fs native promises
This is great news, although still experimental we are finally going to see the fs module with promises. Previously you either had to use the callbacks version (I know you didn’t like it!) or somehow get it promisified.
New console method
Console got a new method which can be helpful if you want to print out data in table format. I’ve put an example below, for more please refer to the official documentation here.
// │ (index) │ username│ password│
// │ 0 │ ‘user1’ │ ‘pass’ │
// │ 1 │ ‘user2’ │ ‘myPass’ │
And of course Node 10 ships out with the latest and greatest npm 6. Happy coding!