mirror of
https://github.com/pnpm/pnpm.git
synced 2026-04-10 18:18:56 -04:00
fix(install): exit with an error when no package.json is found (#4614)
close #4609
This commit is contained in:
6
.changeset/hungry-toes-cheat.md
Normal file
6
.changeset/hungry-toes-cheat.md
Normal file
@@ -0,0 +1,6 @@
|
||||
---
|
||||
"pnpm": major
|
||||
"@pnpm/plugin-commands-installation": patch
|
||||
---
|
||||
|
||||
Exit with an error when running `pnpm install` in a directory that has no `package.json` file in it (and in parent directories) [#4609](https://github.com/pnpm/pnpm/issues/4609).
|
||||
@@ -168,8 +168,8 @@ when running add/update with the --workspace option')
|
||||
|
||||
let { manifest, writeProjectManifest } = await tryReadProjectManifest(opts.dir, opts)
|
||||
if (manifest === null) {
|
||||
if (opts.update) {
|
||||
throw new PnpmError('NO_IMPORTER_MANIFEST', 'No package.json found')
|
||||
if (opts.update === true || params.length === 0) {
|
||||
throw new PnpmError('NO_PKG_MANIFEST', `No package.json found in ${opts.dir}`)
|
||||
}
|
||||
manifest = {}
|
||||
}
|
||||
|
||||
26
packages/plugin-commands-installation/test/install.ts
Normal file
26
packages/plugin-commands-installation/test/install.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import path from 'path'
|
||||
import { add, install } from '@pnpm/plugin-commands-installation'
|
||||
import { prepareEmpty } from '@pnpm/prepare'
|
||||
import { DEFAULT_OPTS } from './utils'
|
||||
|
||||
test('install fails if no package.json is found', async () => {
|
||||
prepareEmpty()
|
||||
|
||||
await expect(install.handler({
|
||||
...DEFAULT_OPTS,
|
||||
dir: process.cwd(),
|
||||
})).rejects.toThrow(/No package\.json found/)
|
||||
})
|
||||
|
||||
test('install does not fail when a new package is added', async () => {
|
||||
prepareEmpty()
|
||||
|
||||
await add.handler({
|
||||
...DEFAULT_OPTS,
|
||||
dir: process.cwd(),
|
||||
}, ['is-positive@1.0.0'])
|
||||
|
||||
const pkg = await import(path.resolve('package.json'))
|
||||
|
||||
expect(pkg?.dependencies).toStrictEqual({ 'is-positive': '1.0.0' })
|
||||
})
|
||||
Reference in New Issue
Block a user