Detalhes do pacote

validate-npm-package-name

npm78.4mISC6.0.0

Give me a string and I'll tell you if it's a valid npm package name

npm, package, names, validation

readme (leia-me)

validate-npm-package-name

Give me a string and I'll tell you if it's a valid npm package name.

This package exports a single synchronous function that takes a string as input and returns an object with two properties:

  • validForNewPackages :: Boolean
  • validForOldPackages :: Boolean

Contents

Naming Rules

Below is a list of rules that valid npm package name should conform to.

  • package name length should be greater than zero
  • all the characters in the package name must be lowercase i.e., no uppercase or mixed case names are allowed
  • package name can consist of hyphens
  • package name must not contain any non-url-safe characters (since name ends up being part of a URL)
  • package name should not start with . or _
  • package name should not contain any spaces
  • package name should not contain any of the following characters: ~)('!*
  • package name cannot be the same as a node.js/io.js core module nor a reserved/blacklisted name. For example, the following names are invalid:
    • http
    • stream
    • node_modules
    • favicon.ico
  • package name length cannot exceed 214

Examples

Valid Names

var validate = require("validate-npm-package-name")

validate("some-package")
validate("example.com")
validate("under_score")
validate("123numeric")
validate("@npm/thingy")
validate("@jane/foo.js")

All of the above names are valid, so you'll get this object back:

{
  validForNewPackages: true,
  validForOldPackages: true
}

Invalid Names

validate("excited!")
validate(" leading-space:and:weirdchars")

That was never a valid package name, so you get this:

{
  validForNewPackages: false,
  validForOldPackages: false,
  errors: [
    'name cannot contain leading or trailing spaces',
    'name can only contain URL-friendly characters'
  ]
}

Legacy Names

In the old days of npm, package names were wild. They could have capital letters in them. They could be really long. They could be the name of an existing module in node core.

If you give this function a package name that used to be valid, you'll see a change in the value of validForNewPackages property, and a warnings array will be present:

validate("eLaBorAtE-paCkAgE-with-mixed-case-and-more-than-214-characters-----------------------------------------------------------------------------------------------------------------------------------------------------------")

returns:

{
  validForNewPackages: false,
  validForOldPackages: true,
  warnings: [
    "name can no longer contain capital letters",
    "name can no longer contain more than 214 characters"
  ]
}

Tests

npm install
npm test

License

ISC

changelog (log de mudanças)

Changelog

6.0.0 (2024-09-24)

⚠️ BREAKING CHANGES

  • validate-npm-package-name now supports node ^18.17.0 || >=20.5.0

    Bug Fixes

  • d14349c #131 align to npm 10 node engine range (@hashtagchris)

    Chores

  • e78d90d #131 run template-oss-apply (@hashtagchris)
  • 3b308e9 #128 bump @npmcli/eslint-config from 4.0.5 to 5.0.0 (@dependabot[bot])
  • 5f444c8 #129 postinstall for dependabot template-oss PR (@hashtagchris)
  • a75975a #129 bump @npmcli/template-oss from 4.23.1 to 4.23.3 (@dependabot[bot])

5.0.1 (2024-05-06)

Dependencies

  • f12f849 #104 replace builtins dependency with Node.js module.builtinModules (#104)

Chores

  • f2b3233 #112 auto publish (#112) (@lukekarrys)
  • 406b31a #110 bump @npmcli/template-oss to 4.22.0 (@lukekarrys)
  • bcc451a #69 update tap coverage in package.json (#69) (@wraithgar)
  • 320e5dd #68 add new tests to reach full test coverage (#68) (@janbritz)
  • 5c72411 #59 bump @npmcli/eslint-config from 3.1.0 to 4.0.0 (@dependabot[bot])
  • a893e39 #110 postinstall for dependabot template-oss PR (@lukekarrys)
  • 13f9b85 #109 bump @npmcli/template-oss from 4.21.3 to 4.21.4 (@dependabot[bot])

5.0.0 (2022-10-10)

⚠️ BREAKING CHANGES

  • validate-npm-package-name is now compatible with the following semver range for node: ^14.17.0 || ^16.13.0 || >=18.0.0

Features

  • f0983ef #50 postinstall for dependabot template-oss PR (@lukekarrys)

4.0.0 (2022-03-29)

⚠ BREAKING CHANGES

  • scopedPackagePattern is no longer exported from this module.
  • This drops support for node 10 and non-LTS versions of node 12 and node 14

Features

Bug Fixes

  • remove validate.scopedPackagePattern (#42) (1943cc6)

Documentation

  • Move invalid example to the right section (#18) (9ee8d54)
  • update misleading naming rule (#27) (3c65535)

Dependencies

3.0.0

Breaking Changes

Docs

Dependencies