nodejs/node · error · Error

Invalid package, must have name and version

Error message

Invalid package, must have name and version

What it means

Thrown by the `npm pack` command when a resolved package manifest lacks `_id`, which is the composite `name@version` string. The manifest is fetched via `pacote.manifest()` from a spec (file path, directory, tarball URL, or registry spec). If the resulting manifest has no `_id`, the package is structurally invalid — it has no name or no version.

Source

Thrown at deps/npm/lib/commands/pack.js:53

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

    const Arborist = require('@npmcli/arborist')
    // Get the manifests and filenames first so we can bail early on manifest errors before making any tarballs
    const manifests = []
    for (const arg of args) {
      const spec = npa(arg)
      const options = isReleaseAgeExcluded(
        trustedSpecName(spec),
        this.npm.flatOptions.minReleaseAgeExclude
      ) ? { ...this.npm.flatOptions, before: null } : this.npm.flatOptions
      const manifest = await pacote.manifest(spec, {
        ...options,
        Arborist,
        preferOnline: true,
        _isRoot: true,
      })
      if (!manifest._id) {
        throw new Error('Invalid package, must have name and version')
      }
      manifests.push({ arg, manifest, options })
    }

    // Load tarball names up for printing afterward to isolate from the noise generated during packing
    const tarballs = []
    for (const { arg, manifest, options } of manifests) {
      const tarballData = await libpack(arg, {
        ...options,
        foregroundScripts: this.npm.config.isDefault('foreground-scripts')
          ? true
          : this.npm.config.get('foreground-scripts'),
        preferOnline: true,
        prefix: this.npm.localPrefix,
        workspaces: this.workspacePaths,
      })
      tarballs.push(await getContents(manifest, tarballData))
    }

View on GitHub (pinned to 1b2de5e052)

Solutions

  1. Ensure package.json has both `name` and `version` fields set
  2. Run `npm init` to generate a valid package.json if one is missing fields
  3. If packing a tarball, verify its internal package.json is well-formed: `tar -xzf pkg.tgz -O package/package.json | jq .`
  4. If packing a remote spec, check the registry/mirror returns complete manifests

Example fix

// before — package.json
{
  "name": "my-package"
  // missing version
}

// after
{
  "name": "my-package",
  "version": "1.0.0"
}
Defensive patterns

Strategy: validation

Validate before calling

function validateManifest(manifest) {
  if (!manifest || !manifest._id) {
    throw new Error('Package manifest must have name and version (_id)')
  }
}
// Before packing:
const manifest = require('./package.json')
if (!manifest.name || !manifest.version) {
  throw new Error('package.json must have name and version fields')
}

Type guard

function hasValidManifest(manifest) {
  return manifest != null
    && typeof manifest.name === 'string' && manifest.name.length > 0
    && typeof manifest.version === 'string' && manifest.version.length > 0
}

Try / catch

try {
  await pack.exec([spec])
} catch (e) {
  if (e.message.includes('must have name and version')) {
    console.error('Ensure package.json has both name and version fields')
  }
  throw e
}

Prevention

When it happens

Trigger: Packing a local directory whose package.json is missing a `name` or `version` field, packing a tarball whose embedded package.json is malformed, or packing a registry spec where the resolved manifest came back without a proper `_id`. The check is `if (!manifest._id)`.

Common situations: Running `npm pack` in a project with a package.json that is missing required fields (name, version). Packing a .tgz created from a broken package. Packing from a registry mirror that returned a truncated manifest. Recently initializing a project without `npm init` leaving a minimal package.json.

Related errors


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