JavaScript Type Testing Utility
The `is` library is a lightweight, comprehensive JavaScript utility designed for type and value testing. It provides a rich API to determine the type or specific characteristics of a variable, offering functions like `is.string()`, `is.array()`, `is.empty()`, `is.equal()`, and many more for common JavaScript types and states. Currently stable at version 3.3.2, the package maintains a steady but not rapid release cadence, primarily focusing on bug fixes and dependency updates. Its key differentiator is the extensive, declarative API that simplifies complex type checks, often replacing verbose `typeof` or `instanceof` chains, making code more readable and robust. It's widely used in both Node.js and browser environments for input validation, conditional logic, and defensive programming.
Common errors
-
TypeError: is.string is not a function
cause Attempting to access a type-checking function directly from a named import or an incorrectly structured CommonJS import.fixEnsure you are importing the entire `is` object as the default export (ESM: `import is from 'is';`) or requiring the entire module (CommonJS: `const is = require('is');`). All type functions are properties of this 'is' object, e.g., `is.string('value')`. -
ERR_REQUIRE_ESM: require() of ES Module ... not supported
cause This library is primarily CommonJS, but if a dependency *within* `is` or another part of your project incorrectly uses `"type": "module"` and is then `require()`-d, this error can occur.fixThis error is typically not caused by `is` itself unless you are using a compromised or improperly bundled version. Verify your project's `package.json` `type` field and module resolution settings. For `is` specifically, ensure you are on a non-compromised version (>=3.3.2) and that your environment correctly handles CJS interop for `require` or uses `import` as appropriate.
Warnings
- breaking A critical supply chain attack occurred, leading to the publication of malicious versions of the 'is' package. Specifically, versions 3.3.1 and 5.0.0 released on July 19, 2025, were compromised and contained malicious code. These versions were promptly removed from npm.
- deprecated Several functions like `is.instanceof`, `is.null`, and `is.undefined` are formally deprecated within the library. While they still function, their use is discouraged.
- gotcha The package does not officially declare itself as an ES Module (`"type": "module"` in `package.json` or explicit `.mjs` files). While modern bundlers and Node.js can typically consume CommonJS modules via `import`, direct ESM consumption might lead to unexpected behavior or require specific tooling configurations.
Install
-
npm install is -
yarn add is -
pnpm add is
Imports
- is
import { is } from 'is'; // Incorrect named import, 'is' is the default export const is = require('is'); // Correct for CommonJSimport is from 'is';
- is.string
import { string } from 'is'; // 'string' is a property of the 'is' object, not a named export const string = require('is').string; // CommonJS, but less common to destructure 'is'import is from 'is'; is.string('hello'); - is.defined
import { defined } from 'is'; // 'defined' is a property of the 'is' object, not a named exportimport is from 'is'; if (is.defined(myVar)) { /* ... */ }
Quickstart
import is from 'is';
function processValue(value) {
if (is.nil(value)) {
console.log('Value is null or undefined.');
return 'default_value';
} else if (is.string(value)) {
console.log(`Value is a string: ${value}`);
return value.trim();
} else if (is.array(value)) {
console.log(`Value is an array with ${value.length} elements.`);
return value.filter(item => is.defined(item));
} else if (is.object(value) && is.empty(value)) {
console.log('Value is an empty object.');
return {};
} else {
console.log(`Value is of type: ${is.type(value)}`);
return value;
}
}
console.log(processValue(null));
console.log(processValue(' hello world '));
console.log(processValue([1, undefined, 3]));
console.log(processValue({}));
console.log(processValue(123));
console.log(processValue(new Date()));