Skip to content

Commit 9c3413f

Browse files
committed
fix: npm link <pkg> should not save package.json
When running `npm link <pkg>` it should not save the new item to the currrent dependencies of that package.json file. Fixes: #2034 Co-authored-by: Darcy Clarke <[email protected]> PR-URL: #2245 Credit: @ruyadorno Close: #2245 Reviewed-by: @darcyclarke
1 parent 523cf69 commit 9c3413f

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

lib/link.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,23 @@ const linkInstall = async args => {
112112
)
113113
}
114114

115+
// npm link should not save=true by default unless you're
116+
// using any of --save-dev or other types
117+
const save =
118+
Boolean(npm.config.find('save') !== 'default' || npm.flatOptions.saveType)
119+
115120
// create a new arborist instance for the local prefix and
116121
// reify all the pending names as symlinks there
117122
const localArb = new Arborist({
118123
...npm.flatOptions,
119124
path: npm.prefix,
125+
save,
120126
})
121127
await localArb.reify({
128+
...npm.flatOptions,
129+
path: npm.prefix,
122130
add: names.map(l => `file:${resolve(globalTop, 'node_modules', l)}`),
131+
save,
123132
})
124133

125134
await reifyFinish(localArb)

test/lib/link.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const npm = {
2323
get () {
2424
return false
2525
},
26+
find () {},
2627
},
2728
}
2829
const printLinks = async (opts) => {
@@ -196,7 +197,7 @@ t.test('link global linked pkg to local nm when using args', (t) => {
196197
})
197198

198199
t.test('link pkg already in global space', (t) => {
199-
t.plan(2)
200+
t.plan(3)
200201

201202
const testdir = t.testdir({
202203
'global-prefix': {
@@ -224,17 +225,26 @@ t.test('link pkg already in global space', (t) => {
224225
npm.globalDir = resolve(testdir, 'global-prefix', 'lib', 'node_modules')
225226
npm.prefix = resolve(testdir, 'my-project')
226227

228+
npm.config.find = () => 'default'
229+
227230
const _cwd = process.cwd()
228231
process.chdir(npm.prefix)
229232

230233
reifyOutput = async () => {
231234
reifyOutput = undefined
232235
process.chdir(_cwd)
236+
npm.config.find = () => null
233237

234238
const links = await printLinks({
235239
path: npm.prefix,
236240
})
237241

242+
t.equal(
243+
require(resolve(testdir, 'my-project', 'package.json')).dependencies,
244+
undefined,
245+
'should not save to package.json upon linking'
246+
)
247+
238248
t.matchSnapshot(links, 'should create a local symlink to global pkg')
239249
}
240250

0 commit comments

Comments
 (0)