nodejs/node · error · Error

Cannot use view command in global mode.

Error message

Cannot use view command in global mode.

What it means

Thrown by `npm view` when the local form (`.` or no package-spec) is requested while npm is running in global mode. Local view reads the current directory's package.json, but in global mode the prefix points at the global node_modules, so reading 'the local package' is meaningless and is disallowed. The check runs before any package.json read.

Source

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

      fullMetadata: true,
      preferOnline: true,
      _isRoot: true,
    }
    const spec = npa(opts.conf.argv.remain[2])
    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)')

View on GitHub (pinned to 1b2de5e052)

Solutions

  1. Drop the global flag: `npm view .` from your package directory.
  2. If you intended a registry package, pass the full spec (`npm view <pkg>`) instead of the local form.
  3. Remove `global=true` from your .npmrc if it was set unintentionally.

Example fix

// before
npm view . --global
// after
npm view .
Defensive patterns

Strategy: validation

Validate before calling

if (isLocalSpec(spec) && npm.config.get('global')) {
  throw new Error('Local view (.) cannot be combined with --global; drop one')
}

Type guard

const isLocalSpec = (spec) => spec === '.' || spec === '' || spec == null

Try / catch

try {
  await view.exec(args)
} catch (err) {
  if (/global mode/i.test(err.message)) {
    // strip the -g flag or replace '.' with an explicit package spec
  } else { throw err }
}

Prevention

When it happens

Trigger: `npm view .` or bare `npm view` (resolved as local) combined with `-g`/`--global`, or with global set via npmrc. parseArgs flagged the spec as local.

Common situations: User has `global=true` in ~/.npmrc and tries `npm view .`; running a script that sets npm_config_global=1; copy-pasting a global flag from an install command.

Related errors


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