n/prefer-global/text-decoder
April 30, 2026 ยท View on GitHub
๐ Enforce either TextDecoder or require("util").TextDecoder.
The TextDecoder class of util module is defined as a global variable.
console.log(TextDecoder === require("util").TextDecoder) //โ true
It will be readable if we use either TextDecoder consistently.
๐ Rule Details
This rule enforces which TextDecoder we should use.
Options
This rule has a string option.
{
"n/prefer-global/text-decoder": ["error", "always" | "never"]
}
"always"(default) ... enforces to use the global variableTextDecoderrather thanrequire("util").TextDecoder."never"... enforces to userequire("util").TextDecoderrather than the global variableTextDecoder.
always
Examples of ๐ incorrect code for this rule:
/*eslint n/prefer-global/text-decoder: [error]*/
const { TextDecoder } = require("util")
const u = new TextDecoder(s)
Examples of ๐ correct code for this rule:
/*eslint n/prefer-global/text-decoder: [error]*/
const u = new TextDecoder(s)
never
Examples of ๐ incorrect code for the "never" option:
/*eslint n/prefer-global/text-decoder: [error, never]*/
const u = new TextDecoder(s)
Examples of ๐ correct code for the "never" option:
/*eslint n/prefer-global/text-decoder: [error, never]*/
const { TextDecoder } = require("util")
const u = new TextDecoder(s)