{"record":{"id":"95a67d4bd5b0b5aa","repo":"nodejs/node","slug":"cannot-use-view-command-in-global-mode","errorCode":null,"errorMessage":"Cannot use view command in global mode.","messagePattern":"Cannot use view command in global mode\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"deps/npm/lib/commands/view.js","lineNumber":58,"sourceCode":"      fullMetadata: true,\n      preferOnline: true,\n      _isRoot: true,\n    }\n    const spec = npa(opts.conf.argv.remain[2])\n    const pckmnt = await packument(spec, config)\n    const defaultTag = npm.config.get('tag')\n    const dv = pckmnt.versions[pckmnt['dist-tags'][defaultTag]]\n    pckmnt.versions = Object.keys(pckmnt.versions).sort(semver.compareLoose)\n\n    return getCompletionFields(pckmnt).concat(getCompletionFields(dv))\n  }\n\n  async exec (args) {\n    let { pkg, local, rest } = parseArgs(args)\n\n    if (local) {\n      if (this.npm.global) {\n        throw new Error('Cannot use view command in global mode.')\n      }\n      const dir = this.npm.prefix\n      const manifest = await readJson(resolve(dir, 'package.json'))\n      if (!manifest.name) {\n        throw new Error('Invalid package.json, no \"name\" field')\n      }\n      // put the version back if it existed\n      pkg = `${manifest.name}${pkg.slice(1)}`\n    }\n\n    await this.#viewPackage(pkg, rest)\n  }\n\n  async execWorkspaces (args) {\n    const { pkg, local, rest } = parseArgs(args)\n\n    if (!local) {\n      log.warn('Ignoring workspaces for specified package(s)')","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/nodejs/node/blob/1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e/deps/npm/lib/commands/view.js#L40-L76","documentation":"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.","triggerScenarios":"`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.","commonSituations":"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.","solutions":["Drop the global flag: `npm view .` from your package directory.","If you intended a registry package, pass the full spec (`npm view <pkg>`) instead of the local form.","Remove `global=true` from your .npmrc if it was set unintentionally."],"exampleFix":"// before\nnpm view . --global\n// after\nnpm view .","handlingStrategy":"validation","validationCode":"if (isLocalSpec(spec) && npm.config.get('global')) {\n  throw new Error('Local view (.) cannot be combined with --global; drop one')\n}","typeGuard":"const isLocalSpec = (spec) => spec === '.' || spec === '' || spec == null","tryCatchPattern":"try {\n  await view.exec(args)\n} catch (err) {\n  if (/global mode/i.test(err.message)) {\n    // strip the -g flag or replace '.' with an explicit package spec\n  } else { throw err }\n}","preventionTips":["Avoid setting `global=true` in .npmrc unless you need it everywhere.","When scripting `npm view .`, ensure no global flag leaks from env.","Prefer explicit package specs in CI for determinism."],"tags":["validation","view","global","cli-args"],"backgroundTag":null,"analyzedSha":"1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e","analyzedAt":"2026-08-13T00:53:24.642Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}