JString Everything Node & Js Related

What’s new in Node.js 8.5?

What's new in Node.js 8.5?

With the recent release of Node.js 8.5, three very exciting new features landed in the Node.js core. In the short blogpost, you are going to learn about them!

ECMAScript Modules in Node.js!

One of the most requested features of Node.js was to provide support for ESM. What it means is that you can now use the import keyword, without transpiling your code. So from now on, you can run snippets like these:

import fs from 'fs'  

The only two things you have to pay attention to is to name your file with the mjs extension and to run Node.js with the --experimental-modules flag.

For now, using ESM has some limitation in Node.js:

You can check the corresponding pull request here: https://github.com/nodejs/node/pull/14369/files. Thanks for the amazing work Bradley Farias, Guy Bedford and Jan Krems!

Performance Hooks

An initial implementation of the Performance Timing API is also shipped with Node.js 8.5.

The User Timing mark() and measure() APIs are implemented, as is a Node.js specific flavour of the Frame Timing for measuring event loop duration.

You can start using it with Node.js 8.5 this way:

const { performance } = require('perf_hooks')  
setTimeout(() => {  
  performance.measure('A to B', 'A', 'B')
  const entry = performance.getEntriesByName('A to B', 'measure')
}, 10000)

To learn more, check out the official documentation here: https://nodejs.org/api/perf_hooks.html.

You can check the corresponding pull request here: https://github.com/nodejs/node/pull/14680/files. Thanks for the amazing work James M Snell!

File copy with the core fs module

With Node.js 8.5, a new File System feature is shipped as well – now you can copy files using the core fs module!

const fs = require('fs')

fs.copyFile('source.txt', 'destination.txt', (err) => {  
  if (err) {
    // handle error properly, not just console.log
    return console.error(err)
  console.log('source.txt was copied to destination.txt')

You can check the corresponding pull request here: https://github.com/nodejs/node/pull/15034/files. Thanks for the amazing work Colin Ihrig!

We hope with these amazing new feature additions you can do even more great libraries and applications using Node.js! Do you already have a library which depends on these features? Let us know in the comments!

You can check out all the fixes and features what Node.js 8.5 brought here: https://nodejs.org/en/blog/release/v8.5.0/.