Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

npm-package-arg

npm72mISC12.0.2TypeScript support: definitely-typed

Parse the things that can be arguments to npm install

readme

npm-package-arg

Build Status

Parses package name and specifier passed to commands like npm install or npm cache add, or as found in package.json dependency sections.

EXAMPLES

const assert = require("assert")
const npa = require("npm-package-arg")

// Pass in the descriptor, and it'll return an object
try {
  const parsed = npa("@bar/foo@1.2")
} catch (ex) {
  …
}

USING

const npa = require('npm-package-arg')

const result = npa(arg[, where])

  • arg - a string that you might pass to npm install, like: `foo@1.2,@bar/foo@1.2,foo@user/foo,http://x.com/foo.tgz`, git+https://github.com/user/foo, bitbucket:user/foo, foo.tar.gz, ../foo/bar/ or bar. If the arg you provide doesn't have a specifier part, eg foo then the specifier will default to latest.
  • where - Optionally the path to resolve file paths relative to. Defaults to process.cwd()

Throws if the package name is invalid, a dist-tag is invalid or a URL's protocol is not supported.

const result = npa.resolve(name, spec[, where])

  • name - The name of the module you want to install. For example: foo or @bar/foo.
  • spec - The specifier indicating where and how you can get this module. Something like: 1.2, ^1.7.17, http://x.com/foo.tgz, git+https://github.com/user/foo, bitbucket:user/foo, file:foo.tar.gz or file:../foo/bar/. If not included then the default is latest.
  • where - Optionally the path to resolve file paths relative to. Defaults to process.cwd()

Throws if the package name is invalid, a dist-tag is invalid or a URL's protocol is not supported.

const purl = npa.toPurl(arg, reg)

Returns the purl (package URL) form of the given package name/spec.

  • arg - A package/version string. For example: `foo@1.0.0or@bar/foo@2.0.0-alpha.1`.
  • reg - Optionally the URL to the package registry. If not specified, assumes the default https://registry.npmjs.org.

Throws if the package name is invalid, or the supplied arg can't be resolved to a purl.

RESULT OBJECT

The objects that are returned by npm-package-arg contain the following keys:

  • type - One of the following strings:
    • git - A git repo
    • tag - A tagged version, like "foo@latest"
    • version - A specific version number, like "foo@1.2.3"
    • range - A version range, like "foo@2.x"
    • file - A local .tar.gz, .tar or .tgz file.
    • directory - A local directory.
    • remote - An http url (presumably to a tgz)
    • alias - A specifier with an alias, like myalias@npm:foo@1.2.3
  • registry - If true this specifier refers to a resource hosted on a registry. This is true for tag, version and range types.
  • name - If known, the name field expected in the resulting pkg.
  • scope - If a name is something like @org/module then the scope field will be set to @org. If it doesn't have a scoped name, then scope is null.
  • escapedName - A version of name escaped to match the npm scoped packages specification. Mostly used when making requests against a registry. When name is null, escapedName will also be null.
  • rawSpec - The specifier part that was parsed out in calls to npa(arg), or the value of spec in calls to npa.resolve(name, spec).
  • saveSpec - The normalized specifier, for saving to package.json files. null for registry dependencies. See note below about how this is (not) encoded.
  • fetchSpec - The version of the specifier to be used to fetch this resource. null for shortcuts to hosted git dependencies as there isn't just one URL to try with them.
  • gitRange - If set, this is a semver specifier to match against git tags with
  • gitCommittish - If set, this is the specific committish to use with a git dependency.
  • hosted - If from === 'hosted' then this will be a hosted-git-info object. This property is not included when serializing the object as JSON.
  • raw - The original un-modified string that was provided. If called as npa.resolve(name, spec) then this will be name + '@' + spec.
  • subSpec - If type === 'alias', this is a Result Object for parsing the target specifier for the alias.

SAVE SPECS

TLDR: file: urls are NOT uri encoded.

Historically, npm would uri decode file package args, but did not do any uri encoding for the saveSpec. This meant that it generated incorrect saveSpecs for directories with characters that looked like encoded uri characters, and also that it could not parse directories with some unencoded uri characters (such as %).

In order to fix this, and to not break all existing versions of npm, this module now parses all file package args as not being uri encoded. And in order to not break all of the package.json files npm has made in the past, it also does not uri encode the saveSpec. This includes package args that start with file:. This does mean that npm file: package args are not RFC compliant, and making them so constitutes quite a breaking change.

changelog

Changelog

12.0.2 (2025-02-05)

Bug Fixes

  • 14cb8a1 #200 properly parse non-url encoded file specs (#200) (@wraithgar)

    Chores

  • 1343a54 #199 bump @npmcli/template-oss from 4.23.4 to 4.23.5 (#199) (@dependabot[bot], @npm-cli-bot)

12.0.1 (2024-12-10)

Bug Fixes

  • ea07a6e #197 allow for git usernames that start with a number (#197) (@wraithgar)

    Chores

  • 41aa799 #196 bump @npmcli/template-oss from 4.23.3 to 4.23.4 (#196) (@dependabot[bot], @npm-cli-bot)

12.0.0 (2024-09-25)

⚠️ BREAKING CHANGES

11.0.3 (2024-07-22)

Bug Fixes

  • 59d53b3 #184 throws an err when alias is without name (#184) (@milaninfy)

Chores

  • 911661e #176 bump @npmcli/template-oss to 4.22.0 (@lukekarrys)
  • 575012e #186 bump @npmcli/template-oss from 4.22.0 to 4.23.1 (#186) (@dependabot[bot], @wraithgar)
  • 74d06ae #176 postinstall for dependabot template-oss PR (@lukekarrys)

11.0.2 (2024-04-12)

Documentation

  • 1765111 #171 readme: fix broken badge URL (#171) (@10xLaCroixDrinker)

Dependencies

Chores

  • 207ba7d #168 postinstall for dependabot template-oss PR (@lukekarrys)
  • 604c1d2 #168 bump @npmcli/template-oss from 4.21.1 to 4.21.3 (@dependabot[bot])
  • 82273b5 #165 postinstall for dependabot template-oss PR (@lukekarrys)
  • 4228b37 #165 bump @npmcli/template-oss from 4.19.0 to 4.21.1 (@dependabot[bot])
  • d4b1447 #147 postinstall for dependabot template-oss PR (@lukekarrys)
  • c5920a9 #147 bump @npmcli/template-oss from 4.18.1 to 4.19.0 (@dependabot[bot])
  • ee68f93 #146 postinstall for dependabot template-oss PR (@lukekarrys)
  • 7901052 #146 bump @npmcli/template-oss from 4.18.0 to 4.18.1 (@dependabot[bot])

11.0.1 (2023-09-05)

Bug Fixes

  • 74b3c7e #141 use URL instead of url.parse (#141) (@wraithgar)

Documentation

  • ea00495 #142 fix readme typo (#142) (@rotu)
  • 26705c5 #143 Fix citations to RFC 8089 (not 8909) for file: url (#143) (@rotu)

11.0.0 (2023-08-15)

⚠️ BREAKING CHANGES

  • the strict RFC 8089 mode has been removed
  • support for node 14 has been removed

Bug Fixes

Dependencies

  • d2ab7ba #138 bump hosted-git-info from 6.1.1 to 7.0.0

10.1.0 (2022-12-01)

Features

  • f2c243c #122 add function to return pacakge purl (@bdehamer, @ljharb)

10.0.0 (2022-10-18)

⚠️ BREAKING CHANGES

  • x and x@ now return the same spec as x@*
  • npm-package-arg is now compatible with the following semver range for node: ^14.17.0 || ^16.13.0 || >=18.0.0

Features

  • 749ccad #104 postinstall for dependabot template-oss PR (@lukekarrys)

Bug Fixes

  • d2b87c0 #97 standardize x x@ and x@* (#97) (@wraithgar)
  • 7b9cb25 #108 resolve relative urls that start with file:// (@lukekarrys)

Dependencies

9.1.0 (2022-06-22)

Features

  • git: add support for :: in #committish (#91) (246f1e9)

9.0.2 (2022-03-29)

Dependencies

  • bump validate-npm-package-name from 3.0.0 to 4.0.0 (#83) (05f40c5)

9.0.1 (2022-03-15)

Dependencies

  • bump hosted-git-info from 4.1.0 to 5.0.0 (#75) (c26876d)

9.0.0 (2022-02-10)

⚠ BREAKING CHANGES

  • This drops support for node10 and non-LTS versions of node 12 and node 14.

Bug Fixes

  • make error message more clear to locate which package is invalid (8cb4527)

Dependencies

Documentation

  • Update result object documentation for type=alias (55907a9)

8.0.0 (2019-12-15)

⚠ BREAKING CHANGES

  • Dropping support for node 6 and 8. It'll probably still work on those versions, but they are no longer supported or tested, since npm v7 is moving away from them.

  • drop support for node 6 and 8 (ba85e68)

7.0.0 (2019-11-11)

deps

BREAKING CHANGES

  • this drops support for ancient node versions.

6.1.1 (2019-08-21)

Bug Fixes

  • preserve drive letter on windows git file:// urls (3909203)

6.1.0 (2018-04-10)

Bug Fixes

  • git: Fix gitRange for git+ssh for private git (#33) (647a0b3)

Features

  • alias: add npm: registry alias spec (#34) (ab99f8e)