支持 ES Module
此次版本迭代中,Node.js終於支持了開發者呼聲最高的 ES 模塊提案。這意味着,你可以直接使用import
關鍵字引入需要的模塊。 Node
8.5 可以運行如下代碼:
1 2 | `import fs from 'fs'` |
使用es模塊,你需要注意,引入文件的擴展名應爲 mjs
,同時使用 --experimental-modules
標識。
在 Node.js 中使用 ES 模塊的限制:
import()
, V8引擎將在下一版本支持,import.meta
, V8引擎暫不支持,- 不支持
require('./foo.mjs')
參考文章:https://github.com/nodejs/node/pull/14369/files
性能監控
在 Node.js 8.5 版本中,新加入性能監控API 。
在 Node.js 8.5 中,可以調用 mark()
和 measure()
API,監控 Node.js 事件執行時間。
在 Node.js 8.5 中,你可以這樣使用:
1
2
3
4
5
6
7
8
9
|
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
模塊添加文件複製功能
Node.js 8.5 推出了更高級的文件系統,在這個版本你可以直接通過 fs
模塊複製某個文件的代碼:
1
2
3
4
5
6
7
8
9
10
|
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
通過這些新特性,Node開發者或許又要迎來第二春。