Viewport Dimensions Utility
This `viewport-dimensions` utility provides a simple API for retrieving and watching browser viewport dimensions (width, height, vmin, vmax). Unlike `window.innerWidth`, it specifically uses `documentElement.clientWidth` and `documentElement.clientHeight` to report dimensions *excluding* scrollbars, which is a key differentiator. This package is currently at version `0.2.0` and appears to be unmaintained, with its last known activity dating back several years. It primarily targets CommonJS environments and does not natively support modern ES module imports. It offers basic functions like `width()`, `height()`, `min()`, `max()`, and `setDimensions()` to update internal references, making it suitable for simple, browser-based dimension tracking without external dependencies. The low version number and lack of recent updates suggest it is no longer actively developed or maintained.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use `require()` in an ES module context in Node.js or in a browser environment without a CommonJS loader/bundler.fixEnsure your environment supports CommonJS or use a bundler (like Webpack/Rollup) that handles CommonJS modules. If in an ESM project, you might need to configure your bundler for CommonJS interop or use `import()` for dynamic loading. -
Cannot read properties of undefined (reading 'width')
cause The 'viewport' object was not successfully loaded or initialized, often due to an incorrect import path, the package not being installed, or attempting to run in a non-browser environment where `document` or `window` are undefined.fixVerify the package is installed (`npm i -S viewport-dimensions`), the import path is `require('viewport')` (not `viewport-dimensions`), and that the code is executing in a browser-like environment. -
Module not found: Error: Can't resolve 'viewport'
cause Incorrect package name in the `require()` statement or the package was not properly installed.fixEnsure the package is installed via `npm i -S viewport-dimensions` and imported specifically as `require('viewport')`, as the internal module name differs from the npm package name.
Warnings
- breaking This package is unmaintained and considered abandoned. It has not received updates for several years, which may lead to compatibility issues with modern browsers or Node.js versions, and potential unpatched vulnerabilities.
- gotcha The package uses `documentElement.clientWidth` and `clientHeight` to determine dimensions, which specifically excludes scrollbar width/height. If you expect `window.innerWidth` or `window.innerHeight` behavior (which includes scrollbars), this will report different values.
- breaking This package is a CommonJS module and does not natively support ES module (`import`/`export`) syntax. Attempting to `import` it directly in an ESM context will result in syntax errors or require specific build tool configurations.
Install
-
npm install viewport-dimensions -
yarn add viewport-dimensions -
pnpm add viewport-dimensions
Imports
- viewport
import viewport from 'viewport-dimensions'; // Incorrect package name and ESM syntax import { viewport } from 'viewport'; // Not a named export, common mistakeconst viewport = require('viewport'); - width
import { width } from 'viewport'; // 'width' is a method on the default export, not a direct named exportconst viewport = require('viewport'); const currentWidth = viewport.width(); - height
import { height } from 'viewport'; // 'height' is a method on the default export, not a direct named exportconst viewport = require('viewport'); const currentHeight = viewport.height();
Quickstart
// This code should be run in a browser environment or with a CommonJS-compatible runtime.
// 1. Install the package: npm install viewport-dimensions
// 2. Use require() to import it.
const viewport = require('viewport');
console.log('Initial Viewport State:');
console.log('Width:', viewport.width());
console.log('Height:', viewport.height());
console.log('Max Dimension (vmin):', viewport.max());
console.log('Min Dimension (vmax):', viewport.min());
// To simulate a resize event and update dimensions
// In a real browser, you would attach this to window.onresize
function updateAndLogDimensions() {
viewport.setDimensions(); // Manually update internal dimension references
console.log('\nViewport dimensions updated:');
console.log('New Width:', viewport.width());
console.log('New Height:', viewport.height());
}
// Call it once for demonstration, or attach to window.resize in a browser
// window.addEventListener('resize', updateAndLogDimensions);
updateAndLogDimensions();
// Example of retrieving a dimension again after potential changes
console.log('\nFinal Width (example):', viewport.width());