1001Ferramentas
📦Dev

NPM Package Specifier Parser

Parse an NPM package spec (lodash, @scope/[email protected], github:user/repo) into name, scope, version and type.

The forms of an npm package specifier

What you write after a package name is not only a version number. npm accepts a range, a dist-tag, a git repository, a tarball URL, a local path, a GitHub shorthand and an alias — and each form resolves differently at install time. The page identifies which one you have in hand and explains what it implies.

The most useful distinction is between an exact version, a range and a dist-tag. An exact version pins; a range lets npm choose at install time; a dist-tag is a published name pointing at a version that can change without notice. There is also a real ambiguity: npm tries to read the text as a range and, failing that, as a dist-tag — so a typo in a range can turn into a search for a tag that does not exist.

The forms that skip the registry deserve attention. A git repository with no ref after the hash installs the default branch, which means the same install at two moments can bring different code. A path with file: creates a symlink rather than copying, so a change in the source folder shows up immediately — excellent in development, bad in a container image.

Frequently asked questions

What is the npm: alias for?
For installing a package under a different name, which solves two problems: using two versions of the same library in one project, and swapping a package for a fork without changing any import. The dependency name is whatever you choose, and npm: names the real package to download.
Is the user-slash-repository shorthand safe?
It works, but it is a prefixless form read as GitHub, and it carries the same problems as any git dependency: without a ref it is not reproducible, and the repository can vanish or change owner. In production, prefer publishing to a registry, even a private one, over pointing at a repository.
What does the lockfile change here?
It pins the version the range resolved to on the first install, along with a hash of the content. So a wide range in the manifest does not mean an unstable install — provided the lockfile is committed and the install uses the command that respects it rather than the one that updates it.

Related Tools