JavaScript Built-in Types List
The `js-types` package provides a comprehensive, programmatically accessible list of built-in JavaScript data types, primarily sourced from MDN Web Docs. It is currently at stable version 4.0.0, which requires Node.js 18.20 and is a pure ESM package. The package maintains a moderate release cadence, with major versions often coinciding with significant Node.js LTS updates and new ECMAScript feature adoptions. Its key differentiator lies in its straightforward design: the core data is simply a `js-types.json` file that can be directly imported, making it highly portable and easy to integrate across various JavaScript environments—from Node.js backend services to browser-side applications—without introducing complex dependencies. This makes it an ideal utility for tasks like runtime type checking, generating documentation, or any scenario requiring a definitive, up-to-date enumeration of JavaScript's fundamental types.
Common errors
-
ERR_REQUIRE_ESM
cause Attempting to use `require()` to import `js-types` in a CommonJS context when the package is pure ESM.fixUpdate your import statement from `const jsTypes = require('js-types');` to `import jsTypes from 'js-types';`. Ensure your project configuration supports ES modules (e.g., `"type": "module"` in `package.json`). -
Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import 'js-types' is not supported resolving ES modules
cause This error can occur if you're trying to import the package using a path like `import 'js-types';` without specifying the default export or if there's a misconfiguration in how your bundler/Node.js resolves ESM.fixAlways use the full import path for the default export: `import jsTypes from 'js-types';` or specific files like `import typesJson from 'js-types/js-types.json';`. -
TypeError: __dirname is not defined in ES module scope
cause When migrating to pure ESM, CommonJS globals like `__dirname`, `__filename`, and `require` are no longer available in the module scope.fixRefactor your code to use ES module equivalents. For `__dirname`, use `import.meta.url` and `path.dirname(fileURLToPath(import.meta.url))`.
Warnings
- breaking Version 4.0.0 of `js-types` is now a pure ECMAScript Module (ESM) and no longer supports CommonJS `require()`. You must update your project to use `import` statements.
- breaking Version 4.0.0 explicitly requires Node.js 18.20 or newer. Running on older Node.js versions will result in errors.
- breaking Version 3.0.0 required Node.js 18 or newer. If you are upgrading from an older version directly to v3.x or v4.x, ensure your Node.js version meets these minimums.
Install
-
npm install js-types -
yarn add js-types -
pnpm add js-types
Imports
- jsTypes
const jsTypes = require('js-types');import jsTypes from 'js-types';
- typesJson
const typesJson = require('js-types/js-types.json');import typesJson from 'js-types/js-types.json';
- Type (TypeScript)
type JSTypeName = typeof import('js-types')[number];
Quickstart
import jsTypes from 'js-types';
console.log('--- All JavaScript Built-in Types ---');
console.log(`Total types found: ${jsTypes.length}`);
console.log(jsTypes.join(', '));
// Example: Check if a specific type is in the list
const typeToCheck = 'Map';
if (jsTypes.includes(typeToCheck)) {
console.log(`\n'${typeToCheck}' is a recognized JavaScript built-in type.`);
} else {
console.log(`\n'${typeToCheck}' is NOT in the list of JavaScript built-in types.`);
}