Skip to content

Commit 91e46a3

Browse files
authored
fix(init): use locally installed version of given package (#7721)
`npm init` calls `libnpmexec` with path and runPath, for most cases it would not matter but when we have a package installed locally on root and we want to run `npm init that-package -w workspace` it should identify that-package is installed and use that to init new workspace package. here the `path` is where node_modules are and `runPath` is cwd to run the command. Fixes: #7700
1 parent 4e81a6a commit 91e46a3

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

lib/commands/init.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class Init extends BaseCommand {
9595
await this.update(workspacesPaths)
9696
}
9797

98-
async execCreate (args, path = process.cwd()) {
98+
async execCreate (args, runPath = process.cwd()) {
9999
const [initerName, ...otherArgs] = args
100100
let packageName = initerName
101101

@@ -129,7 +129,6 @@ class Init extends BaseCommand {
129129
globalBin,
130130
chalk,
131131
} = this.npm
132-
const runPath = path
133132
const scriptShell = this.npm.config.get('script-shell') || undefined
134133
const yes = this.npm.config.get('yes')
135134

@@ -140,7 +139,7 @@ class Init extends BaseCommand {
140139
globalBin,
141140
output,
142141
chalk,
143-
path,
142+
path: this.npm.localPrefix,
144143
runPath,
145144
scriptShell,
146145
yes,

test/lib/commands/init.js

+30
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const t = require('tap')
22
const fs = require('node:fs/promises')
3+
const nodePath = require('node:path')
34
const { resolve, basename } = require('node:path')
45
const _mockNpm = require('../../fixtures/mock-npm')
56
const { cleanTime } = require('../../fixtures/clean-snapshot')
@@ -428,4 +429,33 @@ t.test('workspaces', async t => {
428429
t.equal(ws.version, '1.0.0')
429430
t.equal(ws.license, 'ISC')
430431
})
432+
t.test('init pkg - installed workspace package', async t => {
433+
const { npm } = await mockNpm(t, {
434+
prefixDir: {
435+
'package.json': JSON.stringify({
436+
name: 'init-ws-test',
437+
dependencies: {
438+
'@npmcli/create': '1.0.0',
439+
},
440+
workspaces: ['test/workspace-init-a'],
441+
}),
442+
'test/workspace-init-a': {
443+
'package.json': JSON.stringify({
444+
version: '1.0.0',
445+
name: '@npmcli/create',
446+
bin: { 'init-create': 'index.js' },
447+
}),
448+
'index.js': `#!/usr/bin/env node
449+
require('fs').writeFileSync('npm-init-test-success', '')
450+
console.log('init-create ran')`,
451+
},
452+
},
453+
})
454+
await npm.exec('install', []) // reify
455+
npm.config.set('workspace', ['test/workspace-init-b'])
456+
await npm.exec('init', ['@npmcli'])
457+
const exists = await fs.stat(nodePath.join(
458+
npm.prefix, 'test/workspace-init-b', 'npm-init-test-success'))
459+
t.ok(exists.isFile(), 'bin ran, creating file inside workspace')
460+
})
431461
})

0 commit comments

Comments
 (0)