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.
npm install to-valid-identifierVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to import and use `toValidIdentifier` to convert various strings into valid JavaScript identifiers, showcasing its handling of special characters, reserved words, and ensuring consistent output for identical inputs.
Upgrade your Node.js environment to version 20 or newer. If you cannot upgrade, consider using an older major version of the package (e.g., v0.x).
Upgrade to `to-valid-identifier` v1.0.0 or later to ensure correct encoding of identifiers starting with numbers (e.g., `123name` becomes `_123name`).
Always use the default import syntax: `import toValidIdentifier from 'to-valid-identifier';` for ESM, or `const toValidIdentifier = require('to-valid-identifier');` for CommonJS.Change your import statement from `import { toValidIdentifier } from 'to-valid-identifier';` to `import toValidIdentifier from 'to-valid-identifier';`.Adjust the import statement in your source file to use the default import syntax: `import toValidIdentifier from 'to-valid-identifier';`.
Use `const toValidIdentifier = require('to-valid-identifier');` to correctly assign the directly exported function. Do not use destructuring like `const { toValidIdentifier } = require('to-valid-identifier');`.Update 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.
No dependency data recorded yet.