Skip to content

Commit 6e34dd8

Browse files
author
Michael Smith
committed
feat!: omit license field by default when initializing package.json
BREAKING CHANGE: The license field is no longer included by default when running `npm init` or `npm init --yes`. If you want to include a license, you can either set it in your npm config (`npm set init-license=MIT`) or provide it interactively when running `npm init`.
1 parent e885190 commit 6e34dd8

8 files changed

Lines changed: 64 additions & 14 deletions

File tree

lib/default-input.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,27 @@ if (!package.author) {
262262
: yes ? '' : prompt('author')
263263
}
264264

265-
const license = package.license || getConfig('license') || 'ISC'
266-
exports.license = yes ? license : prompt('license', license, (data) => {
267-
if (validateLicense(data)) {
268-
return data
265+
const configLicense = getConfig('license')
266+
const license = package.license || configLicense || undefined
267+
268+
if (yes) {
269+
// Only include license if explicitly set in config or already in package.json
270+
if (license) {
271+
exports.license = license
269272
}
270-
return invalid('Sorry, license should be a valid SPDX license expression')
271-
})
273+
} else {
274+
exports.license = prompt('license', license || undefined, (data) => {
275+
if (!data) {
276+
return undefined
277+
}
278+
if (validateLicense(data)) {
279+
return data
280+
}
281+
return invalid(
282+
'License should be a valid SPDX license expression'
283+
)
284+
})
285+
}
272286

273287
const type = package.type || getConfig('type') || 'commonjs'
274288
exports.type = yes ? type : prompt('type', type, (data) => {

lib/init-package-json.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ async function init (dir,
100100
delete pkg.content.repository
101101
}
102102

103+
// if no license was explicitly provided, don't include one
104+
if (!pzData.license) {
105+
delete pkg.content.license
106+
}
107+
103108
// readJson filters out empty descriptions, but init-package-json
104109
// traditionally leaves them alone
105110
if (!pkg.content.description) {

test/dependencies.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ t.test('read in dependencies and dev deps', async (t) => {
4242
scripts: { test: 'mocha' },
4343
main: 'index.js',
4444
keywords: [],
45-
license: 'ISC',
4645
dependencies: {
4746
tap: '*',
4847
},

test/license.js

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ t.test('license', async (t) => {
1010
inputs: {
1111
name: 'the-name',
1212
licence: [
13-
[/license: \(.*\) $/, 'Apache'], // invalid license
14-
[/license: \(.*\) $/, 'Apache-2.0'], // license
13+
[/license: $/, 'Apache'], // invalid license
14+
[/license: $/, 'Apache-2.0'], // license
1515
],
1616
},
1717
})
@@ -27,3 +27,39 @@ t.test('license', async (t) => {
2727
}
2828
t.has(data, wanted)
2929
})
30+
31+
t.test('license omitted when left blank', async (t) => {
32+
const { data } = await setup(t, __filename, {
33+
inputs: {
34+
name: 'the-name',
35+
licence: [
36+
[/license: $/, ''], // leave blank
37+
],
38+
},
39+
})
40+
41+
t.equal(data.license, undefined, 'license is omitted when left blank')
42+
})
43+
44+
t.test('license from config', async (t) => {
45+
const { data } = await setup(t, __filename, {
46+
config: { yes: 'yes', 'init-license': 'MIT' },
47+
})
48+
49+
t.equal(data.license, 'MIT', 'uses configured license')
50+
})
51+
52+
t.test('license preserved from existing package.json', async (t) => {
53+
const { data } = await setup(t, __filename, {
54+
testdir: {
55+
'package.json': JSON.stringify({
56+
name: 'existing-package',
57+
version: '1.0.0',
58+
license: 'BSD-3-Clause',
59+
}),
60+
},
61+
config: { yes: 'yes' },
62+
})
63+
64+
t.equal(data.license, 'BSD-3-Clause', 'preserves existing license')
65+
})

test/name-spaces.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ t.test('single space', async t => {
2020
version: '1.0.0',
2121
description: '',
2222
scripts: { test: 'echo "Error: no test specified" && exit 1' },
23-
license: 'ISC',
2423
author: '',
2524
main: 'index.js',
2625
}
@@ -42,7 +41,6 @@ t.test('multiple spaces', async t => {
4241
version: '1.0.0',
4342
description: '',
4443
scripts: { test: 'echo "Error: no test specified" && exit 1' },
45-
license: 'ISC',
4644
author: '',
4745
main: 'index.js',
4846
}

test/name-uppercase.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ t.test('uppercase', async (t) => {
2020
version: '1.0.0',
2121
description: '',
2222
scripts: { test: 'echo "Error: no test specified" && exit 1' },
23-
license: 'ISC',
2423
author: '',
2524
main: 'index.js',
2625
}

test/scope-in-config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ t.test('--yes with scope', async (t) => {
1414
scripts: { test: 'echo "Error: no test specified" && exit 1' },
1515
main: 'index.js',
1616
keywords: [],
17-
license: 'ISC',
1817
}
1918

2019
const { data } = await setup(t, __filename, {

test/yes-defaults.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ t.test('--yes defaults', async (t) => {
1414
scripts: { test: 'echo "Error: no test specified" && exit 1' },
1515
main: 'index.js',
1616
keywords: [],
17-
license: 'ISC',
1817
}
1918

2019
const { data } = await setup(t, __filename, {
2120
config: { yes: 'yes' },
2221
})
2322

2423
t.has(data, EXPECT, 'used the default data')
24+
t.equal(data.license, undefined, 'license is omitted by default')
2525
})

0 commit comments

Comments
 (0)