Registry / serialization / to-valid-identifier

to-valid-identifier

JSON →
library1.0.0jsnpmunverified

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-identifier
INSTALL
IMPORT
SIG · TO-VALID-IDENTIFIE
T
to-valid-identifier
serializationjavascriptv1.0.0
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

toValidIdentifier
✓ import toValidIdentifier from 'to-valid-identifier';
✗ import { toValidIdentifier } from 'to-valid-identifier';
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.
toValidIdentifier
✓ const toValidIdentifier = require('to-valid-identifier');
✗ const { toValidIdentifier } = require('to-valid-identifier');
In CommonJS, the module directly exports the function. Destructuring (`{ toValidIdentifier }`) will result in `undefined` as the value.
toValidIdentifier (Type)
✓ import toValidIdentifier from 'to-valid-identifier'; // Type inference handled automatically
✗ import { ToValidIdentifierType } from 'to-valid-identifier';
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.

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.

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)}`);
Debug
Known issues
breakingVersion 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.
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).
affects: >=1.0.0
gotchaPrior to v1.0.0, identifiers that originally started with a number might not have been correctly handled, leading to invalid JavaScript identifier names.
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`).
affects: <1.0.0
gotchaThe `toValidIdentifier` function is a default export. Attempting to import it using named import syntax (e.g., `import { toValidIdentifier } from 'pkg'`) will result in `undefined`.
fix
Always use the default import syntax: `import toValidIdentifier from 'to-valid-identifier';` for ESM, or `const toValidIdentifier = require('to-valid-identifier');` for CommonJS.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: toValidIdentifier is not a function
Attempting to call `toValidIdentifier` after importing it incorrectly as a named export from an ESM module, resulting in `undefined`.
fix
Change 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
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.
fix
Adjust 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 function
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.
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');`.
Error: The 'to-valid-identifier' package requires Node.js version 20 or greater.
The runtime Node.js version is older than the minimum requirement specified by the package (Node.js 20).
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.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
7 hits · last 30 days
node
6
OpenAI (training)
1
Resources
to-valid-identifier — npm install to-valid-identifier · libregistry