Drop-Nodejs14
Drop-Nodejs14 copied to clipboard
Drop Node.js 14 support
Drop Node.js 14 support in your projects
- 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
Refactoring checklist as of April 2023
- Due to the move from
npm 6tonpm 8, convertpackage_lock.jsontolockfileVersion 2 - Now we can use true
#privateFieldsyntax instead of_pseudoPrivateFieldor closures for "Information Hiding" principle - Change deprecated
cluster.isMastertocluster.isPrimary, changecluster.setupMastertocluster.setupPrimary - Check all
requirecalls 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.statorfs.accessinstead, 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.destroyedinstead of the.abortedproperty, and listen forcloseevent instead ofabortandabortedevents 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.jsas of Node.js 16.0.0; the main entry point should be explicitly defined - Stop using
emitter.listenersproperty; 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 torequestinstance (http.IncomingMessage):response.req - Stop using
node:urlAPI; 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
Thenablesupport 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.setNoDelayanymore - New
os.machine()returns one of machine types as a string:arm,arm64,aarch64,mips,mips64,ppc64,ppc64le,s390,s390x,i386,i686,x86_64
Explore new features
Available in all currently supported Node.js versions as of April 2023
- Use
error.causeto chain errors like:new Error('message', { cause: error }), see docs: https://nodejs.org/api/errors.html#errorcause - Use
AbortControllerandAbortSignalin 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
WeakReferencesandFinalizationRegistry - Use new
Promisemethodany - Use
node:httpimprovements: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:
EventandEventTargetclasses: https://nodejs.org/api/events.html#eventtarget-and-event-api - Discover class
BlockListfromnode:net: https://nodejs.org/api/net.html#class-netblocklist - Discover perf_hooks improvements like
createHistogram,PerformanceMark,getEntries,PerformanceMeasure,PerformanceResourceTiming, multiple changes ofPerformanceObserverandHistogram, 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_threadsfeaturesgetEnvironmentDataandsetEnvironmentData, 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:netchanges:Servereventdrop,socket.connectoptions:noDelay,keepAlive, andkeepAliveInitialDelay,socket.localFamily, andsocket.resetAndDestroy - Discover Promise hooks API from
node:v8m see docs: https://nodejs.org/api/v8.html#promise-hooks
Note that you can't freely use
- Following features are still experimental in at least one of supported node.js versions
--watch,process.getActiveResourcesInfo,fs.cp, andfsPromises.cp,Readablemethodsmap,filter,compose,iterator,forEachand so on - Fetch API is experimental in node.js 16.x and 17.x, and available without
--no-experimental-fetchflag only from 18.0 and above, useundicifrom 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 node.js features instead of dependencies
- Use
nodejs/undiciinstead of npm modulesrequest,axios,node-fetch - Prefer to use
node:child_processandnode:worker_trheadsfor CPU utilization and multithreading instead ofnode:cluster - Prefer to use npm module
ws+ browser native implementation ofWebsocketinstead ofsocket.io - Use native
node:crypto.scriptfor password hashing orargon2from npm instead ofbcryptor other npm modules - Use
node:async_hooksinstead ofzone.jsor deprecated built-in node.jsdomainmodule - Prefer to use
dockerinstead ofpm2orforevernpm modules - Prefer to use
fastifyor nativenode:http+ collection of routesMap<string, Function>instead of archaicexpress,connect,koa - Use
Intland ES6 built-in features instead ofmoment.js - Use
${templateStrings}instead ofejsor other npm modules for templating