nodejs/node · error · Error

Invalid package.json, no "name" field

Error message

Invalid package.json, no "name" field

What it means

Thrown by `npm view` in local mode after successfully reading the directory's package.json but finding no `name` field. Without a name, view cannot construct a package spec to query the registry. This is a package.json validity error, not a registry error.

Source

Thrown at deps/npm/lib/commands/view.js:63

    const pckmnt = await packument(spec, config)
    const defaultTag = npm.config.get('tag')
    const dv = pckmnt.versions[pckmnt['dist-tags'][defaultTag]]
    pckmnt.versions = Object.keys(pckmnt.versions).sort(semver.compareLoose)

    return getCompletionFields(pckmnt).concat(getCompletionFields(dv))
  }

  async exec (args) {
    let { pkg, local, rest } = parseArgs(args)

    if (local) {
      if (this.npm.global) {
        throw new Error('Cannot use view command in global mode.')
      }
      const dir = this.npm.prefix
      const manifest = await readJson(resolve(dir, 'package.json'))
      if (!manifest.name) {
        throw new Error('Invalid package.json, no "name" field')
      }
      // put the version back if it existed
      pkg = `${manifest.name}${pkg.slice(1)}`
    }

    await this.#viewPackage(pkg, rest)
  }

  async execWorkspaces (args) {
    const { pkg, local, rest } = parseArgs(args)

    if (!local) {
      log.warn('Ignoring workspaces for specified package(s)')
      return this.exec([pkg, ...rest])
    }

    const json = this.npm.config.get('json')
    await this.setWorkspaces()

View on GitHub (pinned to 1b2de5e052)

Solutions

  1. Add a `name` field to the local package.json.
  2. Run view from inside the leaf package directory that does have a name.
  3. Specify the package explicitly: `npm view <pkg>` instead of using the local form.

Example fix

// before
// package.json: { "version": "1.0.0" }
npm view .
// after
// package.json: { "name": "my-pkg", "version": "1.0.0" }
npm view .
Defensive patterns

Strategy: validation

Validate before calling

const manifest = JSON.parse(await readFile('package.json', 'utf8'))
if (!manifest.name) {
  throw new Error('package.json is missing a "name" field; cannot view locally')
}

Type guard

const hasNameField = (manifest) =>
  manifest != null && typeof manifest.name === 'string' && manifest.name.length > 0

Try / catch

try {
  await view.exec(['.'])
} catch (err) {
  if (/no .name. field/i.test(err.message)) {
    // read package.json, prompt user for a name, write it back, then retry
  } else { throw err }
}

Prevention

When it happens

Trigger: `npm view .` (or bare view) where the current package.json parses but has no top-level `name`. Common with private root manifests, boilerplate scaffolds, or partially-edited files.

Common situations: A monorepo root package.json marked private with only a workspaces field and no name; a freshly initialized package before naming; an accidental deletion of the name line.

Related errors


AI-assisted analysis of nodejs/node@1b2de5e052 (2026-08-13). Data as JSON: /api/errors/42397b82ae982691. Report an issue: GitHub.