Node.jsv17已经发布,取代v16作为当前版本。新的v17版本提供了一些新特性:基于Promise的其他核心模块API,在错误堆栈末尾添加Node.js版本信息,OpenSSL3.0支持,v8JavaScript引擎更新到9.5。基于Promise的APINode.js项目的一个持续战略计划是为Node.js核心模块提供基于Promise的API支持。最近几年已经为定时器和流模块提供了PromiseAPI支持。Node.jsv17为readline模块提供基于Promise的API支持。该模块提供了一个接口,用于从ReadableStream对象逐行读取数据。结合process.stdin读取用户在终端输入的数据。如下例所示://test.mjsimport*asreadlinefrom"node:readline/promises";import{stdinasinput,stdoutasoutput}from'process';constrl=readline.createInterface({input,output});constanswer=awaitrl。question('《Nodejs技术栈》的域名是什么:');console.log(`答案:${answer}`);rl.close();运行后效果如下:关于readline模块的更多信息,请参考readline_readline。错误堆栈增加Node.js版本堆栈跟踪是诊断应用程序错误信息的重要部分。在Node.jsv17版本中,如果进程由于某些致命错误而退出,错误堆栈的末尾将包含Node.js版本信息。如果要忽略此信息,请在运行时在命令行上指定--no-extra-info-on-fatal-exception标志。OpenSSL3.0支持Node.jsv17,其中包括最近发布的OpenSSL3.0。根据OpenSSL发布政策,OpenSSL1.1.1将在2023-09-11结束支持,也早于Node.jsv18LTS的结束日期。由于OpenSSL3.0添加了对允许的算法和密钥大小的严格限制,预计会对生态系统产生一些影响,因此OpenSSL3.0包含在Node.jsv17版本中,以便在下一个LTS版本之前为用户测试和反馈留出时间。例如,md4是一种在OpenSSL3.0中默认不再允许的算法。如果是Node.js17之前的Node版本,应用程序可以正常运行,但是在Node.jsv17中会抛出一个错误码ERR_OSSL_EVP_UNSUPPORTED错误信息。importcryptofrom'crypto';console.log(crypto.createHash('md4').update('123','utf8').digest('hex'))在Node.jsv17版本下运行后,出现如下错误信息获得。一个临时的解决方案是在运行时添加--openssl-legacy-provider标志,应用程序将不再报错。$node--openssl-legacy-providertest.mjsc58cda49f00748a3bc0fcfa511d516cbV8更新到9.5v88.1版本启用Intl.DisplayNamesAPI,支持语言、地区、货币、脚本四种类型,现在新增两种类型:calendar、dateTimeField、分别返回不同日历类型和日期时间字段的显示名称。有助于国际化应用。constesCalendarNames=newIntl.DisplayNames(['zh'],{type:'calendar'});console.log(esCalendarNames.of('roc'));//ConstenCalendarNames=newIntl.DisplayNames(['en'],{type:'calendar'});console.log(enCalendarNames.of('roc'));//民国日历日期时间字段国际化名称显示。functionprintDate(dateTimeField){console.log(`${dateTimeField.of('year')}${dateTimeField.of('month')}${dateTimeField.of('day')}`);}printDate(newIntl.DisplayNames(['zh'],{type:'dateTimeField'}))//年月日printDate(newIntl.DisplayNames(['en'],{type:'dateTimeField'}))//年月日printDate(newIntl.DisplayNames(['KOR'],{type:'dateTimeField'}))//???printDate(newIntl.DisplayNames(['THA'],{type:'dateTimeField'}))//???ด??น??นIntl.DateTimeFormatAPI在v89.5中为timeZoneName选项增加了四个新值:shortGeneric,longGeneric,shortOffset,longOffset。通过以下代码示例可以看出差异。console.log(newIntl.DateTimeFormat('zh').format(newDate()));//2021/01/01console.log(newIntl.DateTimeFormat('zh',{timeZoneName:'shortGeneric'}).format(newDate()));//2021/01/01中国时间console.log(newIntl.DateTimeFormat('zh',{timeZoneName:'longGeneric'}).format(newDate()));//2021/01/01中国标准时间console.log(newIntl.DateTimeFormat('zh',{timeZoneName:'shortOffset'}).format(newDate()));//2021/01/01GMT+8console.log(newIntl.DateTimeFormat('zh',{timeZoneName:'longOffset'}).format(newDate()));//2021/01/01GMT+08:00有关详细信息,请参阅v89.5发布文档。附加信息根据Node.js发布时间表,Node.jsv12将于2022年4月结束生命周期。Node.jsv16将于2021年10月26日升级为长期支持版本LTS。奇数版本的Node.js不是稳定版本(例如当前的Node.jsv17),生命周期短,不应该在生产环境中使用。如果您不知道Node.js版本信息,不知道如何安装Node.js,请参考《Node.js版本您了解多少?如何选择?》一文。参考https://medium.com/the-node-js-collection/node-js-17-is-here-8dba1e14e382https://nodejs.org/en/blog/release/v17.0.0/
