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