npmdependencies

How to use version range syntax for locally installed packages?


I am developing an npm package. When I want to test the package, I perform an npm pack and then install the tarball in a different workspace, called examples. My examples/package.json file looks like this, assuming my package is called mypackage version 1.2.3:

{
  "dependencies": {
    "mypackage": "file:mypackage-1.2.3.tgz"
  },
  ...
}

This works fine, but I have several versions of mypackage-X.Y.Z.tgz and would like the latest one to be installed in examples/node_modules when running npm i. Is it possible to achieve the behavior of ^1.2.3 or other range syntaxes for local dependencies without updating examples/package.json manually?


Solution

  • Nope, if you use "file:" protocol in dependencies there is no way to use range syntax. That string is interpreted as a local path to a package, so e.g. '^' is interpreted as part of a local path.

    If you want to reduce manual overhead maybe you may have a look to npm link command - https://docs.npmjs.com/cli/v9/commands/npm-link