matchmediaquery
matchmediaquery is an open-source JavaScript library providing isomorphic media query functionality, allowing developers to check CSS media queries both in browser environments and server-side contexts. Originally forked from the `matchmedia` project due to its maintainer's publishing delays, this library aimed to provide an active alternative. However, its last stable version (0.4.2) was published in late 2015, and the project has seen no significant updates or maintenance since. As a result, it is considered abandoned and may not be suitable for modern web development, lacking support for contemporary JavaScript module systems (ESM), TypeScript definitions, or up-to-date media query specifications. Its primary differentiator was its isomorphic capability, which is now often handled by more modern, actively maintained libraries or frameworks.
Common errors
-
ERR_REQUIRE_ESM: require() of ES module /path/to/node_modules/matchmediaquery/index.js from /path/to/your/app.js not supported.
cause Attempting to use `require()` in an ES module (type: 'module' in package.json) environment, or vice-versa, when the library is exclusively CommonJS.fixFor CommonJS-only libraries, ensure your project's `package.json` does NOT have `"type": "module"` if you are using `require()`. If your project is ESM-native, you might need to use dynamic `import('matchmediaquery')` or resort to a bundler to handle CJS modules. -
TypeError: (0 , matchmediaquery_1.matchMediaQuery) is not a function
cause This typically occurs when trying to destructure a CommonJS default export as a named export in an ES module environment, or if a bundler incorrectly transpiles the module.fixImport the library as a default CommonJS export: `const matchMediaQuery = require('matchmediaquery');` if in a CJS context. If using a bundler with ESM, ensure it correctly handles CJS default exports. -
TS2307: Cannot find module 'matchmediaquery' or its corresponding type declarations.
cause The TypeScript compiler cannot locate type definitions for the `matchmediaquery` package.fixCreate a manual type declaration file (e.g., `global.d.ts` or `matchmediaquery.d.ts`) in your project. Add `declare module 'matchmediaquery' { function matchMediaQuery(query: string, options?: object): boolean; export = matchMediaQuery; }` to define its interface.
Warnings
- breaking The library is considered abandoned. Its last update was in 2015, meaning it does not support modern JavaScript features, module systems (ESM), or contemporary CSS media query specifications (e.g., `prefers-color-scheme`, `dynamic-range`). Using it in a modern project will likely lead to compatibility issues and missing functionality.
- gotcha The library is exclusively CommonJS. Attempting to use `import` statements directly in an ESM-native Node.js project or without a bundler configured for CJS interop will result in a module resolution error.
- gotcha There are no official TypeScript type definitions available for `matchmediaquery`. Developers using TypeScript will encounter 'Could not find a declaration file for module' errors or have to manually declare types.
- gotcha The library's server-side simulation relies on specific options (e.g., `width`, `height`, `devicePixelRatio`) being passed to accurately evaluate media queries. Without these, it may make incorrect assumptions or return unexpected results.
Install
-
npm install matchmediaquery -
yarn add matchmediaquery -
pnpm add matchmediaquery
Imports
- matchMediaQueryFunction
import matchMediaQuery from 'matchmediaquery';
const matchMediaQuery = require('matchmediaquery'); - matchMediaQueryFunction (named)
const { matchMediaQuery } = require('matchmediaquery');const matchMediaQuery = require('matchmediaquery'); - Type Definition
import type { MatchMediaQuery } from 'matchmediaquery';/// <reference types="matchmediaquery" />
Quickstart
const matchMediaQuery = require('matchmediaquery');
// Example 1: Basic media query check (client-side/browser context)
// In a browser, this will use window.matchMedia. On the server,
// it simulates based on provided or default properties.
const isSmallScreen = matchMediaQuery('(max-width: 600px)');
console.log(`Is small screen (browser context): ${isSmallScreen}`);
// Example 2: Server-side simulation with specific viewport properties
// This allows you to test media queries on the server without a DOM.
const simulateMobile = matchMediaQuery('(min-width: 320px) and (max-width: 768px)', {
type: 'screen',
width: 375, // Simulate iPhone width
height: 667,
devicePixelRatio: 2
});
console.log(`Is mobile (server simulated): ${simulateMobile}`);
// Example 3: Another server-side check for a larger desktop screen
const simulateDesktop = matchMediaQuery('(min-width: 1024px)', {
type: 'screen',
width: 1280,
height: 800,
devicePixelRatio: 1
});
console.log(`Is desktop (server simulated): ${simulateDesktop}`);