Registry / type-stubs / package-name-regex

package-name-regex

JSON →
library5.0.0jsnpmunverified

This package provides a highly focused and reliable regular expression specifically designed to validate NPM package names according to the official NPM registry rules. It is currently stable at version 5.0.0 and receives regular maintenance, with major version bumps primarily driven by Node.js End-of-Life (EOL) cycles to ensure compatibility with modern runtime environments. The library differentiates itself by offering a pre-tested, up-to-date regex, saving developers from manually crafting and maintaining complex validation logic, which can be prone to errors as NPM naming conventions evolve. It is a lightweight utility intended for scenarios requiring strict package name validation, such as package managers, build tools, or user input forms, and ships with TypeScript types for enhanced developer experience.

npm install package-name-regex
INSTALL
IMPORT
SIG · PACKAGE-NAME-REGEX
P
package-name-regex
type-stubsjavascriptv5.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
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 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

packageNameRegex
import packageNameRegex from 'package-name-regex'
const packageNameRegex = require('package-name-regex')
Since v3.0.0, the package transitioned to ESM. CommonJS `require` will only import the default export as a property of the module object, not directly.
packageNameRegex (named)
import { packageNameRegex } from 'package-name-regex'
import { default as packageNameRegex } from 'package-name-regex'
A named export for `packageNameRegex` was explicitly added in v3.1.1, offering an alternative to the default export. Both are valid.
RegExp type
import type { RegExp } from 'package-name-regex'
While `RegExp` is a global type, the library ships with its own types. For strict type environments or to ensure compatibility with the library's specific typings, you might explicitly import related types.

Demonstrates how to import and use the `packageNameRegex` to test various strings for NPM package name validity.

import packageNameRegex from 'package-name-regex'; console.log('Validating NPM package names:'); console.log(`'some-package' is valid: ${packageNameRegex.test('some-package')}`); console.log(`'example.com' is valid: ${packageNameRegex.test('example.com')}`); console.log(`'@npm/thingy' is valid: ${packageNameRegex.test('@npm/thingy')}`); console.log(`'crazy!' is valid: ${packageNameRegex.test('crazy!')}`); console.log(`'.start-with-period' is valid: ${packageNameRegex.test('.start-with-period')}`); console.log(`'@npm-zors/money!time.js' is valid: ${packageNameRegex.test('@npm-zors/money!time.js')}`); // The regex object can be used directly with standard RegExp methods const matchResult = 'another-package'.match(packageNameRegex); if (matchResult) { console.log(`'another-package' matched: ${matchResult[0]}`); } else { console.log(`'another-package' did not match.`); }
Debug
Known issues
breakingVersion 5.0.0 introduces a breaking change requiring Node.js version 22 or higher. Earlier Node.js versions are no longer supported.
fix
Upgrade your Node.js runtime to version 22 or later. If you need to support older Node.js environments, consider using an earlier major version of `package-name-regex`.
affects: >=5.0.0
breakingVersion 4.0.0 introduced a breaking change by raising the minimum required Node.js version to 18. This impacts environments running older Node.js versions.
fix
Ensure your Node.js environment is at least version 18. For environments that cannot upgrade, pin the `package-name-regex` dependency to `<4.0.0`.
affects: >=4.0.0 <5.0.0
breakingVersion 3.0.0 dropped support for Node.js 12 and transitioned the package to ECMAScript Modules (ESM). This change requires consumer projects to also use ESM or configure their build tools for interoperability.
fix
If using Node.js 12 or earlier, do not upgrade to v3.0.0+. For newer Node.js, ensure your project uses `import` statements or configure your `package.json` with `"type": "module"` for ESM compatibility. For CommonJS projects, use dynamic `import()` or stick to `package-name-regex@<3.0.0`.
affects: >=3.0.0 <4.0.0
gotchaThe package provides a `RegExp` object. To correctly use its `test()` method or other `RegExp` functionalities, ensure you are calling it as a method on the regex object itself, e.g., `packageNameRegex.test(name)`.
fix
Always invoke the `test` method directly on the `packageNameRegex` object. Avoid trying to use it as a standalone function or assuming it returns a boolean directly without the method call.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: packageNameRegex.test is not a function
This usually happens when `packageNameRegex` is not correctly imported as the `RegExp` object itself, or when trying to call `test` on `undefined`.
fix
Ensure you are using `import packageNameRegex from 'package-name-regex';` or `import { packageNameRegex } from 'package-name-regex';` and that your environment supports ESM. Also, verify that the variable you are calling `.test()` on is indeed the imported regex.
SyntaxError: Cannot use import statement outside a module
This error occurs when attempting to use ESM `import` syntax in a CommonJS (CJS) environment after `package-name-regex` v3.0.0.
fix
Either convert your project to an ESM module by adding `"type": "module"` to your `package.json`, use dynamic `import('package-name-regex')` in your CJS code, or downgrade `package-name-regex` to a version prior to 3.0.0 if CJS is strictly required for your project.
Upgrade
Version history
5.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
11 hits · last 30 days
node
10
OpenAI (training)
1
Resources
package-name-regex — npm install package-name-regex · libregistry