JSON to Media Query String Converter
json2mq is a JavaScript utility that translates plain JavaScript objects or JSON structures into valid CSS media query strings. Its current and only stable version is 0.2.0, which was published over eleven years ago, signifying that the package is no longer actively maintained or developed. The library automatically appends 'px' to numeric dimension values, handles various media types (e.g., 'screen', 'handheld'), and supports negations. A key differentiator is its straightforward, declarative input format for generating potentially complex CSS media query syntax, including support for multiple queries via an array of objects. Due to its age, it predates modern JavaScript module systems and TypeScript integration.
Common errors
-
ReferenceError: require is not defined in ES module scope
cause Attempting to use `require()` in an ES module environment (e.g., a Node.js project with `"type": "module"` in package.json or a `.mjs` file).fixIf within Node.js ESM, use `const createRequire = require('module').createRequire; const requireLocal = createRequire(import.meta.url); const json2mq = requireLocal('json2mq');`. For browser environments or bundled ESM, ensure your bundler is configured to handle CommonJS modules. -
TypeError: (0, _json2mq.default) is not a function
cause Incorrectly attempting to import the default CommonJS export of `json2mq` as a named or default ES module import that does not align with how bundlers might transpile it, especially when `esModuleInterop` is not configured or the environment is pure ESM.fixIf using a bundler (like Webpack, Rollup), ensure it's correctly configured to handle CommonJS modules and their default exports. If in Node.js ESM, use the `createRequire` approach. Otherwise, the package might not be compatible with your setup.
Warnings
- breaking This package is abandoned. The latest version (0.2.0) was published 11 years ago and there have been no updates or security patches since. Using it in modern applications may introduce compatibility issues or security vulnerabilities.
- gotcha The package is CommonJS-only (`require()`) and does not support ES Modules (`import`). This means it cannot be directly imported into modern Node.js or browser projects that use ESM without a compatibility layer or bundler configuration.
- gotcha There are no TypeScript type definitions available for this package, either bundled or on DefinitelyTyped. This will result in a poor developer experience for TypeScript users, requiring manual declaration files (`.d.ts`) or `@ts-ignore` directives.
Install
-
npm install json2mq -
yarn add json2mq -
pnpm add json2mq
Imports
- json2mq
import json2mq from 'json2mq';
const json2mq = require('json2mq'); - json2mq
import { json2mq } from 'json2mq';const json2mq = require('json2mq'); json2mq({minWidth: 100});
Quickstart
const json2mq = require('json2mq');
// Basic media query
console.log(json2mq({ minWidth: 768 }));
// Expected: '(min-width: 768px)'
// Complex query with multiple features and units
console.log(json2mq({ screen: true, minWidth: 1024, maxWidth: '120em', orientation: 'landscape' }));
// Expected: 'screen and (min-width: 1024px) and (max-width: 120em) and (orientation: landscape)'
// Multiple distinct media queries in an array
console.log(json2mq([
{ screen: true, minWidth: 100 },
{ handheld: true, orientation: 'landscape' }
]));
// Expected: 'screen and (min-width: 100px), handheld and (orientation: landscape)'