{"record":{"id":"07c027270a7f8738","repo":"nodejs/node","slug":"invalid-package-must-have-name-and-version","errorCode":null,"errorMessage":"Invalid package, must have name and version","messagePattern":"Invalid package, must have name and version","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"deps/npm/lib/commands/pack.js","lineNumber":53,"sourceCode":"    const json = this.npm.config.get('json')\n\n    const Arborist = require('@npmcli/arborist')\n    // Get the manifests and filenames first so we can bail early on manifest errors before making any tarballs\n    const manifests = []\n    for (const arg of args) {\n      const spec = npa(arg)\n      const options = isReleaseAgeExcluded(\n        trustedSpecName(spec),\n        this.npm.flatOptions.minReleaseAgeExclude\n      ) ? { ...this.npm.flatOptions, before: null } : this.npm.flatOptions\n      const manifest = await pacote.manifest(spec, {\n        ...options,\n        Arborist,\n        preferOnline: true,\n        _isRoot: true,\n      })\n      if (!manifest._id) {\n        throw new Error('Invalid package, must have name and version')\n      }\n      manifests.push({ arg, manifest, options })\n    }\n\n    // Load tarball names up for printing afterward to isolate from the noise generated during packing\n    const tarballs = []\n    for (const { arg, manifest, options } of manifests) {\n      const tarballData = await libpack(arg, {\n        ...options,\n        foregroundScripts: this.npm.config.isDefault('foreground-scripts')\n          ? true\n          : this.npm.config.get('foreground-scripts'),\n        preferOnline: true,\n        prefix: this.npm.localPrefix,\n        workspaces: this.workspacePaths,\n      })\n      tarballs.push(await getContents(manifest, tarballData))\n    }","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/nodejs/node/blob/1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e/deps/npm/lib/commands/pack.js#L35-L71","documentation":"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.","triggerScenarios":"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)`.","commonSituations":"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.","solutions":["Ensure package.json has both `name` and `version` fields set","Run `npm init` to generate a valid package.json if one is missing fields","If packing a tarball, verify its internal package.json is well-formed: `tar -xzf pkg.tgz -O package/package.json | jq .`","If packing a remote spec, check the registry/mirror returns complete manifests"],"exampleFix":"// before — package.json\n{\n  \"name\": \"my-package\"\n  // missing version\n}\n\n// after\n{\n  \"name\": \"my-package\",\n  \"version\": \"1.0.0\"\n}","handlingStrategy":"validation","validationCode":"function validateManifest(manifest) {\n  if (!manifest || !manifest._id) {\n    throw new Error('Package manifest must have name and version (_id)')\n  }\n}\n// Before packing:\nconst manifest = require('./package.json')\nif (!manifest.name || !manifest.version) {\n  throw new Error('package.json must have name and version fields')\n}","typeGuard":"function hasValidManifest(manifest) {\n  return manifest != null\n    && typeof manifest.name === 'string' && manifest.name.length > 0\n    && typeof manifest.version === 'string' && manifest.version.length > 0\n}","tryCatchPattern":"try {\n  await pack.exec([spec])\n} catch (e) {\n  if (e.message.includes('must have name and version')) {\n    console.error('Ensure package.json has both name and version fields')\n  }\n  throw e\n}","preventionTips":["Always run `npm init` to create a valid package.json with name and version","Validate package.json before packing: ensure name and version are non-empty strings","When packing tarballs, verify the internal package.json is well-formed"],"tags":["npm","pack","manifest","package-json","validation"],"backgroundTag":null,"analyzedSha":"1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e","analyzedAt":"2026-08-13T00:53:24.642Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}