{"id":13730,"library":"package-name-regex","title":"NPM Package Name Regex Validator","description":"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.","status":"active","version":"5.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/dword-design/package-name-regex","tags":["javascript","match","name","package","regex","regexp","test","validate","typescript"],"install":[{"cmd":"npm install package-name-regex","lang":"bash","label":"npm"},{"cmd":"yarn add package-name-regex","lang":"bash","label":"yarn"},{"cmd":"pnpm add package-name-regex","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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.","wrong":"const packageNameRegex = require('package-name-regex')","symbol":"packageNameRegex","correct":"import packageNameRegex from 'package-name-regex'"},{"note":"A named export for `packageNameRegex` was explicitly added in v3.1.1, offering an alternative to the default export. Both are valid.","wrong":"import { default as packageNameRegex } from 'package-name-regex'","symbol":"packageNameRegex (named)","correct":"import { packageNameRegex } from 'package-name-regex'"},{"note":"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.","symbol":"RegExp type","correct":"import type { RegExp } from 'package-name-regex'"}],"quickstart":{"code":"import packageNameRegex from 'package-name-regex';\n\nconsole.log('Validating NPM package names:');\nconsole.log(`'some-package' is valid: ${packageNameRegex.test('some-package')}`);\nconsole.log(`'example.com' is valid: ${packageNameRegex.test('example.com')}`);\nconsole.log(`'@npm/thingy' is valid: ${packageNameRegex.test('@npm/thingy')}`);\n\nconsole.log(`'crazy!' is valid: ${packageNameRegex.test('crazy!')}`);\nconsole.log(`'.start-with-period' is valid: ${packageNameRegex.test('.start-with-period')}`);\nconsole.log(`'@npm-zors/money!time.js' is valid: ${packageNameRegex.test('@npm-zors/money!time.js')}`);\n\n// The regex object can be used directly with standard RegExp methods\nconst matchResult = 'another-package'.match(packageNameRegex);\nif (matchResult) {\n  console.log(`'another-package' matched: ${matchResult[0]}`);\n} else {\n  console.log(`'another-package' did not match.`);\n}","lang":"typescript","description":"Demonstrates how to import and use the `packageNameRegex` to test various strings for NPM package name validity."},"warnings":[{"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`.","message":"Version 5.0.0 introduces a breaking change requiring Node.js version 22 or higher. Earlier Node.js versions are no longer supported.","severity":"breaking","affected_versions":">=5.0.0"},{"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`.","message":"Version 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.","severity":"breaking","affected_versions":">=4.0.0 <5.0.0"},{"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`.","message":"Version 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.","severity":"breaking","affected_versions":">=3.0.0 <4.0.0"},{"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.","message":"The 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)`.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"search_vec":"'5.0.0':36 'accord':23 'build':115 'bump':44 'compat':57 'complex':84 'convent':96 'craft':81 'current':32 'cycl':54 'date':75 'design':17 'develop':78,128 'differenti':64 'driven':46 'end':50 'end-of-lif':49 'enhanc':127 'ensur':56 'environ':61 'eol':53 'error':92 'evolv':97 'experi':129 'express':15 'focus':11 'form':120 'high':10 'input':119 'intend':103 'javascript':130 'librari':63 'life':52 'lightweight':101 'logic':86 'maintain':83 'mainten':40 'major':42 'manag':114 'manual':80 'match':131 'modern':59 'name':3,22,95,109,132 'node.js':48 'npm':1,20,27,94 'offer':67 'offici':26 'packag':2,7,21,108,113,133 'pre':70 'pre-test':69 'primarili':45 'prone':90 'provid':8 'receiv':38 'regex':4,76,134 'regexp':135 'registri':28 'regular':14,39 'reliabl':13 'requir':106 'rule':29 'runtim':60 'save':77 'scenario':105 'ship':122 'specif':16 'stabl':33 'strict':107 'test':71,136 'tool':116 'type':125 'typescript':124,138 'up-to-d':72 'user':118 'util':102 'valid':5,19,85,110,137 'version':35,43","created_at":"2026-04-20T01:56:00.390680+00:00","updated_at":"2026-04-20T01:56:00.390680+00:00","problems":[{"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.","cause":"This usually happens when `packageNameRegex` is not correctly imported as the `RegExp` object itself, or when trying to call `test` on `undefined`.","error":"TypeError: packageNameRegex.test is not a function"},{"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.","cause":"This error occurs when attempting to use ESM `import` syntax in a CommonJS (CJS) environment after `package-name-regex` v3.0.0.","error":"SyntaxError: Cannot use import statement outside a module"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/dword-design/package-name-regex","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/package-name-regex","openapi_spec":null,"status_page":null,"smithery":null,"categories":["type-stubs","testing"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-17","next_check":"2026-07-18","install_tag":null}}