102

What's new in Node.js 8.5? | @RisingStack

 6 years ago
source link: https://blog.risingstack.com/whats-new-in-node-js-8-5/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Gergely Nemeth's Picture
Gergely Nemeth

Co-Founder of RisingStack, EM at Uber

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:

  • import(), pending newer V8 release used in Node.js,
  • import.meta, waiting for V8 to implement it,
  • require('./foo.mjs'), not supported.

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')
performance.mark('A')
setTimeout(() => {
  performance.mark('B')
  performance.measure('A to B', 'A', 'B')
  const entry = performance.getEntriesByName('A to B', 'measure')
  console.log(entry.duration)
}, 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/.


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK