Convert to Valid JavaScript Identifier
The `to-valid-identifier` package provides a robust and minimalist utility to convert any given string into a syntactically valid JavaScript identifier. It ensures that the resulting identifier is unique for different inputs, effectively handling special characters, reserved keywords, and numbers. The current stable version is `1.0.0`. Maintained by Sindre Sorhus, this package adheres to best practices, ships with TypeScript types, and aims for high stability with infrequent but impactful major releases. Its core differentiator lies in its guarantee of unique identifiers, making it suitable for code generation, transpilation targets, and dynamic variable/property naming where collisions or invalid syntax are critical concerns.
Common errors
-
TypeError: toValidIdentifier is not a function
cause Attempting to call `toValidIdentifier` after importing it incorrectly as a named export from an ESM module, resulting in `undefined`.fixChange your import statement from `import { toValidIdentifier } from 'to-valid-identifier';` to `import toValidIdentifier from 'to-valid-identifier';`. -
TypeError: (0 , to_valid_identifier__WEBPACK_IMPORTED_MODULE_0__.toValidIdentifier) is not a function
cause This Webpack/Babel error indicates that a named export was expected but the module provided a default export instead. Similar to the 'is not a function' error, it's an incorrect import.fixAdjust the import statement in your source file to use the default import syntax: `import toValidIdentifier from 'to-valid-identifier';`. -
Error: 'require('to-valid-identifier')' is not a functioncause In a CommonJS environment, you are trying to destructure the default export, or treating the entire `require()` result as a function when it's an object.fixUse `const toValidIdentifier = require('to-valid-identifier');` to correctly assign the directly exported function. Do not use destructuring like `const { toValidIdentifier } = require('to-valid-identifier');`. -
Error: The 'to-valid-identifier' package requires Node.js version 20 or greater.
cause The runtime Node.js version is older than the minimum requirement specified by the package (Node.js 20).fixUpdate your Node.js installation to version 20 or later using a version manager like `nvm` or by downloading the latest LTS from the official Node.js website.
Warnings
- breaking Version 1.0.0 of `to-valid-identifier` dropped support for older Node.js versions. A minimum of Node.js 20 is now required to run this package.
- gotcha Prior to v1.0.0, identifiers that originally started with a number might not have been correctly handled, leading to invalid JavaScript identifier names.
- gotcha The `toValidIdentifier` function is a default export. Attempting to import it using named import syntax (e.g., `import { toValidIdentifier } from 'pkg'`) will result in `undefined`.
Install
-
npm install to-valid-identifier -
yarn add to-valid-identifier -
pnpm add to-valid-identifier
Imports
- toValidIdentifier
import { toValidIdentifier } from 'to-valid-identifier';import toValidIdentifier from 'to-valid-identifier';
- toValidIdentifier
const { toValidIdentifier } = require('to-valid-identifier');const toValidIdentifier = require('to-valid-identifier'); - toValidIdentifier (Type)
import { ToValidIdentifierType } from 'to-valid-identifier';import toValidIdentifier from 'to-valid-identifier'; // Type inference handled automatically
Quickstart
import toValidIdentifier from 'to-valid-identifier';
// Basic conversion
console.log(toValidIdentifier('hello world')); // => 'hello$j$world'
// Handles hyphens and special characters
console.log(toValidIdentifier('my-variable-name!')); // => 'my$j$variable$j$name$a$'
// Ensures uniqueness, even for reserved words or problematic starts
console.log(toValidIdentifier('undefined')); // => '$_undefined$'
console.log(toValidIdentifier('123abc')); // => '$123abc$'
// Demonstrates consistent output for specific inputs
const input1 = 'foo-bar';
const input2 = 'foo bar';
const input3 = 'foo-bar'; // Same input, same output
console.log(`'${input1}' => ${toValidIdentifier(input1)}`);
console.log(`'${input2}' => ${toValidIdentifier(input2)}`);
console.log(`'${input3}' => ${toValidIdentifier(input3)}`);