nodejs/node · error · Error

Package name must be specified either as an argument or in p

Error message

Package name must be specified either as an argument or in package.json file

What it means

Thrown by TrustCommand.flagsToOptions when neither a positional package name nor a package.json `name` field is available. Identical root cause to error 62/63 but reached via the create flow: pkgName = pkgPositional || pkgJsonName, and both are undefined. The guard fires before file/entity checks, so it is the first thing flagsToOptions validates.

Source

Thrown at deps/npm/lib/trust-cmd.js:273

    const usedPkgNameFromPkgJson = !pkgPositional && Boolean(pkgJsonName)
    const invalidPkgJsonProviderType = matchPkg && git && git?.type !== name

    let entity
    let entitySource

    if (flags[entityKey]) {
      entity = flags[entityKey]
      entitySource = 'flag'
    } else if (!invalidPkgJsonProviderType && git?.repository) {
      entity = git.repository
      entitySource = 'package.json'
    }
    const mismatchPkgJsonRepository = matchPkg && git && entity !== git.repository
    const usedRepositoryInPkgJson = entitySource === 'package.json'

    const warnings = []
    if (!pkgName) {
      throw new Error('Package name must be specified either as an argument or in package.json file')
    }

    if (!flags.file) {
      throw new Error(`${providerFile} must be specified with the file option`)
    }
    if (!flags.file.endsWith('.yml') && !flags.file.endsWith('.yaml')) {
      throw new Error(`${providerFile} must end in .yml or .yaml`)
    }

    this.validateFile?.(flags.file)

    if (invalidPkgJsonProviderType) {
      const message = this.warnString`Repository in package.json is not a ${providerEntity}`
      if (!flags[entityKey]) {
        throw new Error(message)
      } else {
        warnings.push(message)
      }

View on GitHub (pinned to 1b2de5e052)

Solutions

  1. Pass the package name as the first positional: `npm trust gitlab @scope/pkg --file ... --project ...`.
  2. Add a `name` field to the local package.json.
  3. Run the command from the package root that contains a valid package.json.

Example fix

// before
npm trust gitlab --file .gitlab-ci.yml --project g/p --allow-publish
// after
npm trust gitlab @scope/pkg --file .gitlab-ci.yml --project g/p --allow-publish
Defensive patterns

Strategy: validation

Validate before calling

const pkgName = positionalArg || (await readPkgJsonSafe()).name
if (!pkgName) {
  throw new Error('Provide the package name positionally or via package.json#name')
}

Type guard

const hasUsableName = (positional, pkg) =>
  Boolean(positional) || Boolean(pkg && pkg.name)

Try / catch

try {
  await createConfigCommand(...)
} catch (err) {
  if (/Package name must be specified/i.test(err.message)) {
    // supply a positional package name and retry
  } else { throw err }
}

Prevention

When it happens

Trigger: Running `npm trust <provider> --file ... --project ...` from a directory with no package.json (or one missing `name`) and no positional package argument. optionalPkgJson returns {} on read failure.

Common situations: Setting up trust for a package whose checkout isn't present; running from a docs/scripts folder; a scaffolded package.json without a name yet.

Related errors


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