node:querystring
Node.js legacy query-string module, resolvable via both ESM import and CommonJS
require. Provides percent-encoding and key-value parsing/formatting utilities.
For new code the WHATWG URLSearchParams API is generally preferred, but
querystring remains the canonical Node.js primitive and is frequently required
by existing npm packages.
Available methods
| Method | Description |
|---|---|
parse(str, sep?, eq?, options?) | Parse a URL-encoded query string into a key→value object |
stringify(obj, sep?, eq?, options?) | Serialize an object into a URL-encoded query string |
escape(str) | Percent-encode a string (encodeURIComponent-compatible unreserved set) |
unescape(str) | Percent-decode a string (UTF-8-aware) |
encode(...) | Alias for stringify |
decode(...) | Alias for parse |
Behaviour notes
- Percent-encoding leaves the ECMAScript
encodeURIComponentunreserved set untouched:A-Za-z0-9-._~!*'(). Every other byte is percent-encoded as%XXof its UTF-8 representation. parseconverts+to space in keys and values (Node.js legacy behaviour);unescapedoes not.- Duplicate keys in the input to
parseproduce an array value; single occurrences produce a scalar string. - Array values passed to
stringifyexpand into repeatedkey=valuesegments separated bysep. - Custom delimiters — pass
sepand/oreqto override the defaults (&and=). - Result shape —
parsereturns a plain object with enumerable, writable, configurable data properties, compatible withObject.keys,JSON.stringify, and spread syntax.
Example
javascript
const qs = require("node:querystring");
// parse
const parsed = qs.parse("name=Ada&role=engineer&role=manager");
// → { name: "Ada", role: ["engineer", "manager"] }
// stringify
const encoded = qs.stringify({ name: "Ada", role: ["engineer", "manager"] });
// → "name=Ada&role=engineer&role=manager"
// escape / unescape
qs.escape("a b&c"); // → "a%20b%26c"
qs.unescape("a%20b"); // → "a b"
// custom delimiters
qs.parse("a:1;b:2", ";", ":"); // → { a: "1", b: "2" }See also
- Node.js upstream:
- WHATWG
URLSearchParams(recommended for new code)