fix(env): fail with a meaningful error when no pnpm home dir is found (#6134)

close #6095
close #3865
This commit is contained in:
Zoltan Kochan
2023-02-26 05:39:44 +02:00
committed by GitHub
parent cdd5eb6b5a
commit e570adc101
3 changed files with 25 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/plugin-commands-env": patch
"pnpm": patch
---
`pnpm env -g` should fail with a meaningful error message if pnpm cannot find the pnpm home directory, which is the directory into which Node.js is installed.

View File

@@ -87,6 +87,11 @@ export async function handler (opts: NvmNodeCommandOptions, params: string[]) {
hint: help(),
})
}
if (opts.global && !opts.bin) {
throw new PnpmError('CANNOT_MANAGE_NODE', 'Unable to manage Node.js because pnpm was not installed using the standalon installation script', {
hint: 'If you want to manage Node.js with pnpm, you need to remove any Node.js that was installed by other tools, then install pnpm using one of the standalone scripts that are provided on the installation page: https://pnpm.io/installation',
})
}
switch (params[0]) {
case 'use': {
return envUse(opts, params.slice(1))

View File

@@ -265,3 +265,17 @@ describe('env list', () => {
expect(versions.every(version => semver.satisfies(version, '16'))).toBeTruthy()
})
})
test('fail if there is no global bin directory', async () => {
tempDir()
await expect(
env.handler({
// @ts-expect-error
bin: undefined,
global: true,
pnpmHomeDir: process.cwd(),
rawConfig: {},
}, ['use', 'lts'])
).rejects.toEqual(new PnpmError('CANNOT_MANAGE_NODE', 'Unable to manage Node.js because pnpm was not installed using the standalon installation script'))
})