qss: Tiny Query String Serializer
qss (query string stringify) is a minimalist, 305-byte browser utility designed for efficiently serializing JavaScript objects into URL query strings and parsing query strings back into objects. The current stable version is 3.0.0. It distinguishes itself by its extreme lightness and strong performance for decoding compared to other libraries like `qs`, `query-string`, and even Node.js's native `querystring.decode` in browser contexts. While suitable for browsers, its `encode` function is significantly slower than Node.js's native `querystring.stringify`, making it less ideal for server-side encoding. qss ships with TypeScript type definitions, providing a robust development experience. Releases are feature-driven rather than on a strict cadence, with major versions introducing breaking changes as needed for improvements or modernizations.
Common errors
-
TypeError: qss is not a function or TypeError: qss_1.default is not a function
cause Attempting to use `qss` as a default export after upgrading to v2.0.0 or later.fixUpdate your import statement to use named exports: `import { encode, decode } from 'qss'`. -
ERR_PACKAGE_PATH_NOT_EXPORTED
cause Using `qss` in Node.js versions 13.0-13.7 due to the `exports` map in `package.json` introduced in v3.0.0.fixUpgrade your Node.js runtime to a stable, actively maintained LTS version (e.g., Node.js 14 or newer). -
Query string is missing separator or malformed after encoding.
cause Incorrect usage of the `prefix` argument in `qss.encode`, expecting it to automatically add `?` or `&`.fixEnsure the `prefix` argument includes the full desired prefix with its own separator, e.g., `encode(obj, '?')` or `encode(obj, 'foo=bar&')`.
Warnings
- breaking Version 3.0.0 introduced 'exports' map support, which may cause module loading issues in Node.js versions 13.0 through 13.7. These were development releases and are officially End-of-Life.
- breaking Version 2.0.0 removed the default export. The primary `stringify` functionality is now available as the named `encode` export.
- gotcha qss is optimized for browser environments. While it works in Node.js, the native `querystring.stringify` is significantly faster for encoding operations in Node.js, making qss less performant for server-side encoding tasks.
- gotcha The `prefix` argument for `qss.encode` is appended directly. No checks or automatic glue characters (like `?` or `&`) are added. If you want a prefix, it must include your desired joiner.
Install
-
npm install qss -
yarn add qss -
pnpm add qss
Imports
- encode
import qss from 'qss'
import { encode } from 'qss' - decode
const { decode } = require('qss')import { decode } from 'qss' - qss (CommonJS)
const { encode, decode } = require('qss')
Quickstart
import { encode, decode } from 'qss';
// Example 1: Basic object encoding
const params1 = { foo: 'hello world', bar: [1, 2, 3], baz: true };
const queryString1 = encode(params1);
console.log('Encoded 1:', queryString1);
// Expected: 'foo=hello%20world&bar=1&bar=2&bar=3&baz=true'
// Example 2: Encoding with a custom prefix
const params2 = { limit: 10, offset: 0 };
const queryString2 = encode(params2, '?');
console.log('Encoded 2 with prefix:', queryString2);
// Expected: '?limit=10&offset=0'
// Example 3: Decoding a query string
const decodedParams1 = decode('foo=hello%20world&bar=1&bar=2&bar=3&baz=true');
console.log('Decoded 1:', decodedParams1);
// Expected: { foo: 'hello world', bar: [1, 2, 3], baz: true }
// Example 4: Decoding from location.search (browser context)
// In a browser, you might do:
// const browserQueryString = location.search.substring(1);
// const decodedBrowserParams = decode(browserQueryString);
// console.log('Decoded from browser search:', decodedBrowserParams);