Check ECMAScript Identifier Names
estree-util-is-identifier-name is a focused utility from the unified collective designed to determine whether a given string or Unicode code point can constitute a valid ECMAScript (JavaScript) identifier name. The package, currently at stable version 3.0.0, provides functions like `name` to validate full identifier strings, and `start` and `cont` to check individual code points for starting or continuing an identifier. Major releases, such as v3.0.0, typically coincide with updates to Node.js compatibility (now requiring Node.js 16+) and significant internal changes like the adoption of `exports` for module resolution and enhanced Unicode support for astrals and code points. A key differentiator is its strict adherence to ECMAScript identifier rules, including optional support for JSX identifiers, while intentionally not handling language keywords. It's an ESM-only package since v2.0.0 and ships with full TypeScript types.
Common errors
-
ERR_REQUIRE_ESM
cause Attempting to `require()` the package in a CommonJS module after version 2.0.0.fixChange `const { name } = require('estree-util-is-identifier-name')` to `import { name } from 'estree-util-is-identifier-name'` and ensure your project is configured for ES Modules. -
TypeError: The 'code' argument must be an integer
cause Passing non-numeric values or incorrect types to `start` or `cont` functions, or using `String.prototype.charCodeAt()` which returns UTF-16 code units (char codes) instead of Unicode code points for astral characters in v3.0.0.fixEnsure you pass a valid Unicode code point number (an integer) to `start` and `cont`. Use `String.prototype.codePointAt(index)` to get the correct code point for a character, especially for characters outside the Basic Multilingual Plane. -
SyntaxError: Named export 'default' not found (module 'estree-util-is-identifier-name.js')
cause Attempting to use a default import, but the package only provides named exports.fixChange `import name from 'estree-util-is-identifier-name'` to `import { name } from 'estree-util-is-identifier-name'`.
Warnings
- breaking Version 3.0.0 and above require Node.js 16 or later. Older Node.js versions are no longer supported.
- breaking The `cont` and `start` functions in v3.0.0 now expect Unicode code points as input, not char codes. Passing char codes (e.g., from `String.prototype.charCodeAt()`) will lead to incorrect results or unexpected behavior for astral characters.
- breaking The package switched to ESM-only in v2.0.0. Attempting to `require()` this package in a CommonJS context will result in an error.
- breaking The package now uses `exports` for module resolution, which may break paths for users relying on internal or non-standard module imports. Avoid using private APIs or deeply nested paths.
Install
-
npm install estree-util-is-identifier-name -
yarn add estree-util-is-identifier-name -
pnpm add estree-util-is-identifier-name
Imports
- name
import name from 'estree-util-is-identifier-name'
import { name } from 'estree-util-is-identifier-name' - start
const start = require('estree-util-is-identifier-name').startimport { start } from 'estree-util-is-identifier-name' - cont
import * as estreeUtils from 'estree-util-is-identifier-name'; const { cont } = estreeUtils;import { cont } from 'estree-util-is-identifier-name' - Options
import { Options } from 'estree-util-is-identifier-name'import type { Options } from 'estree-util-is-identifier-name'
Quickstart
import { cont, name, start } from 'estree-util-is-identifier-name';
// Check if a string is a valid identifier name
console.log('Is "$something69" a valid name?', name('$something69')); // => true
console.log('Is "69" a valid name?', name('69')); // => false
console.log('Is "var" a valid name (keywords are not handled here)?', name('var')); // => true
// Check if a code point can start an identifier
// 48 is the Unicode code point for '0'
console.log('Can code point 48 (for "0") start an identifier?', start(48)); // => false
// 97 is the Unicode code point for 'a'
console.log('Can code point 97 (for "a") start an identifier?', start(97)); // => true
// Check if a code point can continue an identifier
// 48 is the Unicode code point for '0'
console.log('Can code point 48 (for "0") continue an identifier?', cont(48)); // => true
// Example with JSX option for names that include hyphens
console.log('Is "my-component" a valid name (without JSX)?', name('my-component')); // => false
console.log('Is "my-component" a valid name (with JSX)?', name('my-component', { jsx: true })); // => true