- Now: April 2023, we have Node.js 18.16.0 LTS, 16.20.0 (maintenance) and 20.0.0 (latest)
- Going to drop Node.js 14 support in all HowProgrammingWorks and Node.js courses, Metarhia codebase
- Now we can rely on Node.js 16.20.0 capabilities
- See release schedule: https://github.com/nodejs/release#release-schedule
- Due to the move from
npm 6
tonpm 8
, convertpackage_lock.json
tolockfileVersion 2
- Now we can use true
#privateField
syntax instead of_pseudoPrivateField
or closures for "Information Hiding" principle - Change deprecated
cluster.isMaster
tocluster.isPrimary
, changecluster.setupMaster
tocluster.setupPrimary
- Check all
require
calls to usenode:
prefix for all internal node.js modules, for example:node:fs
- Use
crypto.randomUUID()
to generate UUID instead of manual generation or third-party modules - Stop using
fs.exists
; usefs.stat
orfs.access
instead, see example:
const toBool = [() => true, () => false];
const exists = await fs.promises.access(filePath).then(...toBool);
- Stop using
fs.rmdir(path, { recursive: true })
; usefs.rm(path, { recursive: true })
instead, see docs: https://nodejs.org/api/fs.html#fsrmpath-options-callback - Check
stream.destroyed
instead of the.aborted
property, and listen forclose
event instead ofabort
andaborted
events forClientRequest
,ServerResponse
, andIncomingMessage
- Stop using deprecated
Server.connections
; useServer.getConnections()
instead, see docs: https://nodejs.org/api/net.html#servergetconnectionscallback - Do not use default
index.js
as of Node.js 16.0.0; the main entry point should be explicitly defined - Stop using
emitter.listeners
property; useemitter.listeners(eventName)
andevents.getEventListeners(emitter, eventName)
instead to get copy of listeners collection, see docs: https://nodejs.org/api/events.html#eventsgeteventlistenersemitterortarget-eventname - Now
response
(http.ServerResponse) has a reference torequest
instance (http.IncomingMessage):response.req
- Stop using
node:url
API; use JavaScript URL class instead - Note that unhandled promise rejections are deprecated and will terminate the process with a non-zero exit code. Add a
process.on('uncaughtException', (reason, origin) => {})
handler to prevent process termination - Stop using deprecated
process.on('multipleResolves', handler)
- Don't change
process.config
(frozen) - Check for deprecated
Thenable
support in streams - Ensure only integer values are used for
process.exit(code)
andprocess.exitCode
- The well-known MODP groups modp1, modp2, and modp5 are deprecated due to their lack of security against practical attacks
- Use
http.createServer({ noDelay, keepAlive, keepAliveInitialDelay })
, no need inrequest.setNoDelay
anymore - New
os.machine()
returns one of machine types as a string:arm
,arm64
,aarch64
,mips
,mips64
,ppc64
,ppc64le
,s390
,s390x
,i386
,i686
,x86_64
Available in all currently supported Node.js versions as of April 2023
- Use
error.cause
to chain errors like:new Error('message', { cause: error })
, see docs: https://nodejs.org/api/errors.html#errorcause - Use
AbortController
andAbortSignal
in different async I/O APIs:exec/fork/spawn
,events.once
,readFile
,stream.Writable/Readable
, and so on - Use
AsyncLocalStorage
,AsyncResource
, see docs: https://nodejs.org/api/async_context.html - Use multiple new promisified APIs:
node:stream/promises
,node:dns/promises
,node:assert/strict
- Use
node:timers/promises
, for exampleawait setTimeout(100)
- Use
WeakReferences
andFinalizationRegistry
- Use new
Promise
methodany
- Use
node:http
improvements:server.maxRequestsPerSocket
,response.strictContentLength
,message.headersDistinct
,message.trailersDistinct
,outgoingMessage.appendHeader
,http.setMaxIdleHTTPParsers
, see docs: https://nodejs.org/api/http.html - Discover multiple improvements in
node:crypto
- Discover multiple improvements in Event API:
Event
andEventTarget
classes: https://nodejs.org/api/events.html#eventtarget-and-event-api - Discover class
BlockList
fromnode:net
: https://nodejs.org/api/net.html#class-netblocklist - Discover perf_hooks improvements like
createHistogram
,PerformanceMark
,getEntries
,PerformanceMeasure
,PerformanceResourceTiming
, multiple changes ofPerformanceObserver
andHistogram
, newperf_hooks.performance
: https://nodejs.org/api/perf_hooks.html - Discover new Web Crypto API: https://nodejs.org/api/webcrypto.html
- Discover new
node:worker_threads
featuresgetEnvironmentData
andsetEnvironmentData
, new classesBroadcastChannel
,EventTarget
, andMessagePort
, see docs: https://nodejs.org/api/worker_threads.html - Discover Node.js native test runner from
node:test
: https://nodejs.org/api/test.html - Take a look at the Diagnostics Channel API: https://nodejs.org/api/diagnostics_channel.html
- See
node:net
changes:Server
eventdrop
,socket.connect
options:noDelay
,keepAlive
, andkeepAliveInitialDelay
,socket.localFamily
, andsocket.resetAndDestroy
- Discover Promise hooks API from
node:v8
m see docs: https://nodejs.org/api/v8.html#promise-hooks
- Following features are still experimental in at least one of supported node.js versions
--watch
,process.getActiveResourcesInfo
,fs.cp
, andfsPromises.cp
,Readable
methodsmap
,filter
,compose
,iterator
,forEach
and so on - Fetch API is experimental in node.js 16.x and 17.x, and available without
--no-experimental-fetch
flag only from 18.0 and above, useundici
from npm for previous node.js versions: https://github.com/nodejs/undici - ESM and CJS loaders API is currently being redesigned and will still change
- Startup Snapshot API and Web Streams API are still experimental, see docs: https://nodejs.org/api/v8.html#startup-snapshot-api
- Use
nodejs/undici
instead of npm modulesrequest
,axios
,node-fetch
- Prefer to use
node:child_process
andnode:worker_trheads
for CPU utilization and multithreading instead ofnode:cluster
- Prefer to use npm module
ws
+ browser native implementation ofWebsocket
instead ofsocket.io
- Use native
node:crypto.script
for password hashing orargon2
from npm instead ofbcrypt
or other npm modules - Use
node:async_hooks
instead ofzone.js
or deprecated built-in node.jsdomain
module - Prefer to use
docker
instead ofpm2
orforever
npm modules - Prefer to use
fastify
or nativenode:http
+ collection of routesMap<string, Function>
instead of archaicexpress
,connect
,koa
- Use
Intl
and ES6 built-in features instead ofmoment.js
- Use
${templateStrings}
instead ofejs
or other npm modules for templating