• Home
Name Date Size #Lines LOC

..--

LICENSED12-May-202411.1 KiB203169

README.mdD12-May-20242.5 KiB11495

index.jsD12-May-20241.9 KiB8779

package.jsonD12-May-20242 KiB7170

README.md

1validate-npm-package-license
2============================
3
4Give me a string and I'll tell you if it's a valid npm package license string.
5
6```javascript
7var valid = require('validate-npm-package-license');
8```
9
10SPDX license identifiers are valid license strings:
11
12```javascript
13
14var assert = require('assert');
15var validSPDXExpression = {
16  validForNewPackages: true,
17  validForOldPackages: true,
18  spdx: true
19};
20
21assert.deepEqual(valid('MIT'), validSPDXExpression);
22assert.deepEqual(valid('BSD-2-Clause'), validSPDXExpression);
23assert.deepEqual(valid('Apache-2.0'), validSPDXExpression);
24assert.deepEqual(valid('ISC'), validSPDXExpression);
25```
26The function will return a warning and suggestion for nearly-correct license identifiers:
27
28```javascript
29assert.deepEqual(
30  valid('Apache 2.0'),
31  {
32    validForOldPackages: false,
33    validForNewPackages: false,
34    warnings: [
35      'license should be ' +
36      'a valid SPDX license expression (without "LicenseRef"), ' +
37      '"UNLICENSED", or ' +
38      '"SEE LICENSE IN <filename>"',
39      'license is similar to the valid expression "Apache-2.0"'
40    ]
41  }
42);
43```
44
45SPDX expressions are valid, too ...
46
47```javascript
48// Simple SPDX license expression for dual licensing
49assert.deepEqual(
50  valid('(GPL-3.0-only OR BSD-2-Clause)'),
51  validSPDXExpression
52);
53```
54
55... except if they contain `LicenseRef`:
56
57```javascript
58var warningAboutLicenseRef = {
59  validForOldPackages: false,
60  validForNewPackages: false,
61  spdx: true,
62  warnings: [
63    'license should be ' +
64    'a valid SPDX license expression (without "LicenseRef"), ' +
65    '"UNLICENSED", or ' +
66    '"SEE LICENSE IN <filename>"',
67  ]
68};
69
70assert.deepEqual(
71  valid('LicenseRef-Made-Up'),
72  warningAboutLicenseRef
73);
74
75assert.deepEqual(
76  valid('(MIT OR LicenseRef-Made-Up)'),
77  warningAboutLicenseRef
78);
79```
80
81If you can't describe your licensing terms with standardized SPDX identifiers, put the terms in a file in the package and point users there:
82
83```javascript
84assert.deepEqual(
85  valid('SEE LICENSE IN LICENSE.txt'),
86  {
87    validForNewPackages: true,
88    validForOldPackages: true,
89    inFile: 'LICENSE.txt'
90  }
91);
92
93assert.deepEqual(
94  valid('SEE LICENSE IN license.md'),
95  {
96    validForNewPackages: true,
97    validForOldPackages: true,
98    inFile: 'license.md'
99  }
100);
101```
102
103If there aren't any licensing terms, use `UNLICENSED`:
104
105```javascript
106var unlicensed = {
107  validForNewPackages: true,
108  validForOldPackages: true,
109  unlicensed: true
110};
111assert.deepEqual(valid('UNLICENSED'), unlicensed);
112assert.deepEqual(valid('UNLICENCED'), unlicensed);
113```
114