What's New in Node 10 "Dubnium"

Publikováno: 26.4.2018

Node.js 10.0.0 is the seventh major Node.js release since the launch of the Node.js Foundation. In October of 2018, it will become the next Active Long Term Support branch.

Celý článek

Node.js 10.0.0 is the seventh major Node.js release since the launch of the Node.js Foundation. In October of 2018, it will become the next Active Long Term Support branch.

Node.js version 10 is out. Yay!! This is the next LTS (Long term support) version of Node codename "Dubnium" (it's a rare radioactive element and the most stable isotope in nature, probably a nod to the stability of Node.js v10).

You can also keep up with the release of Node.js.

There are quite a few cool features with this release so let's just jump into it.

Stable HTTP/2

HTTP/2 support was first released in version 8 of Node. The initial release was a bit buggy, but with this version comes stability of HTTP/2 in Node.

Currently, Hapi and Koa support HTTP/2 in Node v10, but you can still use express-http2-workaround for express.

Let's see a quick example of how to HTTP/2 in Node using Hapi.js:

const fs = require('fs')
const Hapi = require('hapi')
const http2 = require('http2')

const server = new Hapi.Server()
server.connection({
  listener: http2.createServer(),
  host: 'localhost',
  port: 3000
})

server.route({
  method: 'GET',
  path:'/hello',
  handler: function (request, reply) {
    return reply('Hello, World!')
  }
})

server.start((err) => {
  if (err) console.error(err)
  console.log(`Started ${server.connections.length} connections`)
})

Better support for ESM modules

Node came with it's own module system and it's what we've used for years. Node's module system is called CommonJS (you've probably heard of it).

However, CommonJS is not quite common as it's only suited to the Node environment. If you try to run a CommonJS file in the browser, you'll get a barrage of red flags in devtools.

Along comes EcmaScript 6 which brought a module system to JavaScript. With this release Node.js supports this module system.

What this means is that we can now do stuff like

import { resolve } from 'path'

and

export { default } from 'os'

For this to work, you need to adhere to the new .mjs file naming convention. You can read all about it on GitLayer

We also get a standardized global object (window alternative)

Standardized error codes

Before v10, once a new or strange error occurred, we literally had to copy-paste the message in Google and rely on their impeccable ability to find your answer in StackOverflow's archive.

With this release, all errors in node have a code attached to them. This gives us more error handling power and flexibility and removes a lot of guessing from your StackOverflow game. Node Foundation did a better job explaining it, so go check it out.

N-API is no longer experimental

If you don't know what N-API is let me explain.

N-API is an API for building native Addons

You don't have to know any C or C++ to do this as it is a part of Node.js. It is intended to insulate Addons from changes in the underlying JavaScript engine and allow modules compiled for one version to run on later versions of Node.js without recompilation.

It was first released in Node v8, but it's now stable to use.

V8 and OpenSSL updates

Node.js runs on the V8 JavaScript engine, the same engine that runs Google's Chrome. With Node v10, V8 got an upgrade bump to v6.6. Doing this means that we now have support for async generators and iterators in Node. No transpilation or experimental flag necessary.

OpenSSL also got a bump to v1.1.0. This means that we have access to TLS 1.3 which allows faster SSL handshakes and more security. It also means we get the ChaCha20 cipher and Poly1305 authenticator.

npm v6

It's not a Node update without npm. With Node v10 comes npm v6 and npm stepped up their game. This new release comes with

  • More focus on security, and npm has it baked in.
  • You can use the npm audit command to find security vulnerabilities.
  • I haven't tested this out, but it claims to be 17x faster.
  • There's also npm optimizations for continuous integration (CI) environments so builds can run faster.
  • More visible integrity metadata for package tampering. etc.

New methods

New methods added to Node v10 include:

  • String#trimStart/trimEnd to remove unwanted characters either at the beginning or end of strings.
  • RegExp: s (dotAll) & u (unicode) flags, named captures, lookbehinds
  • Optional (e) in try {} catch(e) {}
  • Finally, console.table.
  • EventEmitter.prototype.off() will serve as an alias for EventEmitter.prototype.removeListener()
  • The WHATWG URL API is now a global. Meaning we have access to the same URL API as the browser.

Deprecations

  • Using require() to access several of Node.js' own internal dependencies will emit a runtime deprecation.
  • The crypto.createCipher() and crypto.createDecipher() methods have been deprecated.
  • Passing more than one argument to assert.fail() will emit a runtime deprecation warning
  • Use of non-string values for process.env has been deprecated in documentation.

Conclusion

That was a quick wrap of what's new in Node.js v10. You can read the official changelog for more information on this release.

Below are also a few resources you could check for more information.

Have a great day. Oh! let us know your thoughts on this release in the comments. What's the most exciting feature?

Nahoru
Tento web používá k poskytování služeb a analýze návštěvnosti soubory cookie. Používáním tohoto webu s tímto souhlasíte. Další informace