Node.js 8.5正式发布,新特性一览

开发 前端
最近 Node.js 发布了8.5版本,在这个版本里,Node 添加了3个激动人心的新特性。

[[204585]]

Node.js 8.5 新特性

最近 Node.js 发布了8.5版本,在这个版本里,Node 添加了3个激动人心的新特性。

支持 ES Module

此次版本迭代中,Node 终于支持了开发者呼声***的 ES 模块提案。这意味着,你可以直接使用import关键字引入需要的模块。 Node 8.5 可以运行如下代码:

  1. `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. const { performance } = require('perf_hooks')   
  2. performance.mark('A')   
  3. setTimeout(() => {   
  4.   performance.mark('B'
  5.   performance.measure('A to B''A''B'
  6.   const entry = performance.getEntriesByName('A to B''measure'
  7.   console.log(entry.duration) 
  8. }, 10000) 

 

官方文档:https://nodejs.org/api/perf_hooks.html

参考文章:https://github.com/nodejs/node/pull/14680/files

fs 模块添加文件复制功能

Node.js 8.5 推出了更高级的文件系统,在这个版本你可以直接通过 fs 模块复制某个文件的代码:

  1. const fs = require('fs'
  2.   
  3. fs.copyFile('source.txt''destination.txt', (err) => {   
  4.   if (err) { 
  5.     // handle error properly, not just console.log 
  6.     return console.error(err) 
  7.   } 
  8.   console.log('source.txt was copied to destination.txt'
  9. }) 

 

参考文章:https://github.com/nodejs/node/pull/15034/files

希望通过这些新特性,开发者能做出更令人惊喜的 Node.js 应用。

参考文章:https://nodejs.org/en/blog/release/v8.5.0/ 

责任编辑:庞桂玉 来源: 前端大全
相关推荐

2010-05-11 13:50:26

Scala

2010-07-20 10:19:06

Wine 1.2

2023-09-21 11:12:25

2010-07-27 09:34:15

FreeBSD 8.1

2013-10-18 14:23:21

Ubuntu 13.1Kbuntu 13.1

2011-05-26 10:27:37

Fedora 15

2010-06-01 09:52:27

jQuery Tool

2013-06-13 10:14:33

2013-11-26 10:00:09

VMware Hori虚拟化Wmware

2011-12-16 09:24:53

JavaSpring开源框架

2013-11-26 10:00:30

VMware Hori

2023-04-19 08:31:57

Node.jsLTS版本

2017-10-24 14:57:12

前端Vue 2.5新功能特性

2011-10-19 08:52:59

Android 4.0新特性

2010-01-26 17:44:32

Visual C++开

2022-06-27 06:02:27

geopandas开发Python

2023-11-23 10:21:11

ECMAScriptJavaScript

2012-01-12 16:16:20

Fedora 17特性规划

2013-09-29 16:32:32

欧朋浏览器

2009-04-03 09:13:17

Eclipse插件EclipseEC2
点赞
收藏

51CTO技术栈公众号