UNPKG

1.63 kBMarkdownView Raw
1# validate-npm-package-name
2
3Give me a string and I'll tell you if it's a valid npm package name.
4
5This package exports a single synchronous function that takes a string as
6input and returns an object:
7
8### Valid Names
9
10```js
11var validate = require("validate-npm-package-name")
12
13validate("some-package")
14validate("example.com")
15validate("under_score")
16validate("123numeric")
17validate("crazy!")
18validate("@npm/thingy")
19validate("@jane/foo.js")
20```
21
22All of the above names are valid, so you'll get this object back:
23
24```js
25{
26 validForNewPackages: true,
27 validForOldPackages: true
28}
29```
30
31### Invalid Names
32
33```js
34 validate(" leading-space:and:weirdchars")
35```
36
37That was never a valid package name, so you get this:
38
39```js
40{
41 validForNewPackages: false,
42 validForOldPackages: false,
43 errors: [
44 'name cannot contain leading or trailing spaces',
45 'name can only contain URL-friendly characters'
46 ]
47}
48```
49
50### Legacy Names
51
52In the old days of npm, package names were wild. They could have capital
53letters in them. They could be really long. They could be the name of an
54existing module in node core.
55
56If you give this function a package name that **used to be valid**, you'll see
57a change in the value of `validForNewPackages` property, and a warnings array
58will be present:
59
60```js
61validate("cRaZY-paCkAgE-with-mixed-case-and-more-than-fifty-characters")
62```
63
64returns:
65
66```js
67{
68 validForNewPackages: false,
69 validForOldPackages: true,
70 warnings: [
71 "name can no longer contain capital letters",
72 "name can no longer contain more than 50 characters"
73 ]
74}
75```
76
77## Tests
78
79```sh
80npm install
81npm test
82```
83
84## License
85
86ISC