{"id":12151,"library":"to-valid-identifier","title":"Convert to Valid JavaScript Identifier","description":"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.","status":"active","version":"1.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/sindresorhus/to-valid-identifier","tags":["javascript","valid","identifier","safe","sanitize","variable","property","ecmascript","typescript"],"install":[{"cmd":"npm install to-valid-identifier","lang":"bash","label":"npm"},{"cmd":"yarn add to-valid-identifier","lang":"bash","label":"yarn"},{"cmd":"pnpm add to-valid-identifier","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The `toValidIdentifier` function is exported as the default. Using named import syntax (`{ toValidIdentifier }`) will result in `undefined` or a runtime error in most ESM environments.","wrong":"import { toValidIdentifier } from 'to-valid-identifier';","symbol":"toValidIdentifier","correct":"import toValidIdentifier from 'to-valid-identifier';"},{"note":"In CommonJS, the module directly exports the function. Destructuring (`{ toValidIdentifier }`) will result in `undefined` as the value.","wrong":"const { toValidIdentifier } = require('to-valid-identifier');","symbol":"toValidIdentifier","correct":"const toValidIdentifier = require('to-valid-identifier');"},{"note":"The package ships TypeScript types. When importing `toValidIdentifier` as a default export, its type is automatically inferred. There is no commonly exposed named type export from this package for its primary function.","wrong":"import { ToValidIdentifierType } from 'to-valid-identifier';","symbol":"toValidIdentifier (Type)","correct":"import toValidIdentifier from 'to-valid-identifier'; // Type inference handled automatically"}],"quickstart":{"code":"import toValidIdentifier from 'to-valid-identifier';\n\n// Basic conversion\nconsole.log(toValidIdentifier('hello world')); // => 'hello$j$world'\n\n// Handles hyphens and special characters\nconsole.log(toValidIdentifier('my-variable-name!')); // => 'my$j$variable$j$name$a$'\n\n// Ensures uniqueness, even for reserved words or problematic starts\nconsole.log(toValidIdentifier('undefined')); // => '$_undefined$'\nconsole.log(toValidIdentifier('123abc')); // => '$123abc$'\n\n// Demonstrates consistent output for specific inputs\nconst input1 = 'foo-bar';\nconst input2 = 'foo bar';\nconst input3 = 'foo-bar'; // Same input, same output\n\nconsole.log(`'${input1}' => ${toValidIdentifier(input1)}`);\nconsole.log(`'${input2}' => ${toValidIdentifier(input2)}`);\nconsole.log(`'${input3}' => ${toValidIdentifier(input3)}`);","lang":"typescript","description":"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."},"warnings":[{"fix":"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).","message":"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.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Upgrade to `to-valid-identifier` v1.0.0 or later to ensure correct encoding of identifiers starting with numbers (e.g., `123name` becomes `_123name`).","message":"Prior to v1.0.0, identifiers that originally started with a number might not have been correctly handled, leading to invalid JavaScript identifier names.","severity":"gotcha","affected_versions":"<1.0.0"},{"fix":"Always use the default import syntax: `import toValidIdentifier from 'to-valid-identifier';` for ESM, or `const toValidIdentifier = require('to-valid-identifier');` for CommonJS.","message":"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`.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change your import statement from `import { toValidIdentifier } from 'to-valid-identifier';` to `import toValidIdentifier from 'to-valid-identifier';`.","cause":"Attempting to call `toValidIdentifier` after importing it incorrectly as a named export from an ESM module, resulting in `undefined`.","error":"TypeError: toValidIdentifier is not a function"},{"fix":"Adjust the import statement in your source file to use the default import syntax: `import toValidIdentifier from 'to-valid-identifier';`.","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.","error":"TypeError: (0 , to_valid_identifier__WEBPACK_IMPORTED_MODULE_0__.toValidIdentifier) is not a function"},{"fix":"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');`.","cause":"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.","error":"Error: 'require('to-valid-identifier')' is not a function"},{"fix":"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.","cause":"The runtime Node.js version is older than the minimum requirement specified by the package (Node.js 20).","error":"Error: The 'to-valid-identifier' package requires Node.js version 20 or greater."}],"ecosystem":"npm"}