Node.js 8.5正式發(fā)布,新特性一覽
Node.js 8.5 新特性
最近 Node.js 發(fā)布了8.5版本,在這個(gè)版本里,Node 添加了3個(gè)激動(dòng)人心的新特性。
支持 ES Module
此次版本迭代中,Node 終于支持了開發(fā)者呼聲***的 ES 模塊提案。這意味著,你可以直接使用import關(guān)鍵字引入需要的模塊。 Node 8.5 可以運(yùn)行如下代碼:
- `import fs from 'fs'`
使用es模塊,你需要注意,引入文件的擴(kuò)展名應(yīng)為 mjs,同時(shí)使用 --experimental-modules標(biāo)識(shí)。
在 Node.js 中使用 ES 模塊的限制:
- import(), V8引擎將在下一版本支持,
- import.meta, V8引擎暫不支持,
- 不支持 require('./foo.mjs')
參考文章:https://github.com/nodejs/node/pull/14369/files
性能監(jiān)控
在 Node.js 8.5 版本中,性能監(jiān)控API 。
在 Node.js 8.5 中,可以調(diào)用 mark() 和 measure() API,監(jiān)控 Node.js 事件執(zhí)行時(shí)間。
在 Node.js 8.5 中,你可以這樣使用:
- 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)
官方文檔:https://nodejs.org/api/perf_hooks.html
參考文章:https://github.com/nodejs/node/pull/14680/files
fs 模塊添加文件復(fù)制功能
Node.js 8.5 推出了更高級(jí)的文件系統(tǒng),在這個(gè)版本你可以直接通過 fs 模塊復(fù)制某個(gè)文件的代碼:
- 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')
- })
參考文章:https://github.com/nodejs/node/pull/15034/files
希望通過這些新特性,開發(fā)者能做出更令人驚喜的 Node.js 應(yīng)用。
參考文章:https://nodejs.org/en/blog/release/v8.5.0/