Disallow deprecated APIs (n/no-deprecated-api)
💼 This rule is enabled in the following configs: 🟢 recommended-module, ✅ recommended-script.
Node has many deprecated API. The community is going to remove those API from Node in future, so we should not use those.
This only supports the static apis, eg Buffer.from is supported, but Buffer#includes is not.
📖 Rule Details
Examples of 👎 incorrect code for this rule:
/*eslint n/no-deprecated-api: "error" */
var fs = require("fs");
fs.exists("./foo.js", function() {}); /*ERROR: 'fs.exists' was deprecated since v4. Use 'fs.stat()' or 'fs.access()' instead.*/
// Also, it can report the following patterns.
var exists = require("fs").exists; /*ERROR: 'fs.exists' was deprecated since v4. Use 'fs.stat()' or 'fs.access()' instead.*/
const {exists} = require("fs"); /*ERROR: 'fs.exists' was deprecated since v4. Use 'fs.stat()' or 'fs.access()' instead.*/
// And other deprecated API below.This rule reports the following deprecated API.
- _linklist
- _stream_wrap
- async_hooks
- buffer
- Buffer constructors (Use safe-buffer module for
Node@<4.5.0) - SlowBuffer class
- Buffer constructors (Use safe-buffer module for
- constants (undocumented)
- crypto
- _toBuf
Credentials(undocumented)- DEFAULT_ENCODING
- createCredentials
- createCipher
- createDecipher
- fips
- prng
- pseudoRandomBytes
- rng
- domain
- events
- freelist (undocumented)
- fs
SyncWriteStream(undocumented)- exists
- lchmod
- lchmodSync
- globals
- http
- module
- createRequireFromPath
requireRepl(undocumented)- _debug
- net
- os
tmpDir(undocumented)getNetworkInterfaces(undocumented)
- path
- process
EventEmitter(undocumented)assert(undocumented)- binding
- punycode
- readline
codePointAt(undocumented)getStringWidth(undocumented)isFullWidthCodePoint(undocumented)stripVTControlCharacters(undocumented)
- repl
- sys
- timers
enroll(undocumented)unenroll(undocumented)
- tls
- CleartextStream (this class was removed on v0.11.3, but never deprecated in documents)
- CryptoStream
- SecurePair
convertNPNProtocols(undocumented)- createSecurePair
- parseCertString
- tty
- url
- util
- vm
⚠️ Note that userland modules don't hide core modules. For example,
require("punycode")still imports the deprecated core module even if you executednpm install punycode. Userequire("punycode/")to import userland modules rather than core modules.
Configured Node.js version range
Configured Node.js version range
Options
This rule has 4 options.
{
"rules": {
"n/no-deprecated-api": ["error", {
"version": ">=16.0.0",
"ignoreModuleItems": [],
"ignoreGlobalItems": []
"ignoreIndirectDependencies": true
}]
}
}version
As mentioned above, this rule reads the [engines] field of package.json. But, you can overwrite the version by version option.
The version option accepts the valid version range of node-semver.
ignoreModuleItems
This is the array of module names and module's member names. Default is an empty array.
This rule ignores APIs that ignoreModuleItems includes. This option can include the following values:
_linklist_stream_wrapasync_hooks.currentIdasync_hooks.triggerIdbuffer.Buffer()new buffer.Buffer()buffer.SlowBufferconstantscrypto._toBufcrypto.Credentialscrypto.DEFAULT_ENCODINGcrypto.createCiphercrypto.createCredentialscrypto.createDeciphercrypto.fipscrypto.prngcrypto.pseudoRandomBytescrypto.rngdomainevents.EventEmitter.listenerCountevents.listenerCountfreelistfs.SyncWriteStreamfs.existsfs.lchmodfs.lchmodSynchttp.createClientmodule.Module.createRequireFromPathmodule.createRequireFromPathmodule.Module.requireReplmodule.requireReplmodule.Module._debugmodule._debugnet._setSimultaneousAcceptsos.tmpDirpath._makeLongprocess.EventEmitterprocess.assertprocess.bindingprocess.env.NODE_REPL_HISTORY_FILEprocess.report.triggerReportpunycodereadline.codePointAtreadline.getStringWidthreadline.isFullWidthCodePointreadline.stripVTControlCharacterssystimers.enrolltimers.unenrolltls.CleartextStreamtls.CryptoStreamtls.SecurePairtls.convertNPNProtocolstls.createSecurePairtls.parseCertStringtty.setRawModeurl.parseurl.resolveutil.debugutil.errorutil.isArrayutil.isBooleanutil.isBufferutil.isDateutil.isErrorutil.isFunctionutil.isNullutil.isNullOrUndefinedutil.isNumberutil.isObjectutil.isPrimitiveutil.isRegExputil.isStringutil.isSymbolutil.isUndefinedutil.logutil.printutil.pumputil.putsutil._extendvm.runInDebugContext
Examples of 👍 correct code for the {"ignoreModuleItems": ["new buffer.Buffer()"]}:
/*eslint n/no-deprecated-api: [error, {ignoreModuleItems: ["new buffer.Buffer()"]}] */
const buffer = require("buffer")
const data = new buffer.Buffer(10) // OK since it's in ignoreModuleItems.ignoreGlobalItems
This is the array of global variable names and global variable's member names. Default is an empty array.
This rule ignores APIs that ignoreGlobalItems includes. This option can include the following values:
Buffer()new Buffer()COUNTER_NET_SERVER_CONNECTIONCOUNTER_NET_SERVER_CONNECTION_CLOSECOUNTER_HTTP_SERVER_REQUESTCOUNTER_HTTP_SERVER_RESPONSECOUNTER_HTTP_CLIENT_REQUESTCOUNTER_HTTP_CLIENT_RESPONSEIntl.v8BreakIteratorrequire.extensionsprocess.EventEmitterprocess.assertprocess.bindingprocess.env.NODE_REPL_HISTORY_FILE
Examples of 👍 correct code for the {"ignoreGlobalItems": ["new Buffer()"]}:
/*eslint n/no-deprecated-api: [error, {ignoreGlobalItems: ["new Buffer()"]}] */
const data = new Buffer(10) // OK since it's in ignoreGlobalItems.⚠️ Known Limitations
This rule cannot report the following cases:
non-static properties
- async_hooks
- buffer
- cluster
- crypto
- http
- net
- repl
replServer.convertToContext(undocumented)- replServer.turnOffEditorMode
- replServer.memory
types of arguments
- fs
fs.truncate()andfs.truncateSync()usage with a file descriptor has been deprecated.
- url
url.format()with legacyurlObjecthas been deprecated.
dynamic things
require(foo).aDeprecatedProperty;
require("http")[A_DEPRECATED_PROPERTY]();assignments to properties
var obj = {
Buffer: require("buffer").Buffer
};
new obj.Buffer(); /* missing. */var obj = {};
obj.Buffer = require("buffer").Buffer
new obj.Buffer(); /* missing. */giving arguments
(function(Buffer) {
new Buffer(); /* missing. */
})(require("buffer").Buffer);reassignments
var Buffer = require("buffer").Buffer;
Buffer = require("another-buffer");
new Buffer(); /*ERROR: 'buffer.Buffer' constructor was deprecated.*/