Npm Including all range of pre-release when defining peer-dependency

Viewed 1072

I am maintaining an NPM package (lets say it as package-A) that relies on an another NPM package(package-B) to function. Therefore, I am in need to add that package as peer-dependency into the package.json, so that npm and the user can be sure that everything is correct.

The problem is, package-B use pre-release versioning. I am well aware that it uses semver wrong, but I am not able to change the company policy, yet. Meanwhile enforcing to use proper versioning will happen. The semantic is like this:

  • 1.0.1-alpha.X -> Unstable releases for testing and bleeding edge content
  • 1.0.1-beta.X -> For every seemingly stable version (like rc)
  • 1.0.2 -> When the package is production ready.

So basically, the patch version increases when there is another production release.

Case: Because of the versioning, I need to include every package except major one to match as peer dependency, together with pre-releases.

The need is basically ^1.0.0 with everything including pre-releases that happens to have 1 as major as peer dependency

  • ^1.0.0 -> Does not include pre-releases
  • ^1.0.0 || >=1.0.0-beta.X -> Does not include e.g. 1.0.1-beta.1
  • * -> Does not include pre-releases
  • ^1.0.0 || >=1.X.X-beta.X -> Does not work.

On semver, there is a parameter called --include-prerelease which I think does what I need, specific to semver commands obviously.

react package on NPM has similar versioning system with proper usage of pre-release. E.g. it has 16.0.0, 16.0.0-alpha.1 and 16.6.0-alpha.0. I basically need to include all this on a single range.

Disclaimer: Minor version is changed when there is a breaking change going on. Yet again, I am well aware that this versioning is not up to semver regulations and advising it unfortunately will not solve the problem at hand.

2 Answers

I don't know if this applicable to this use case, but in situations where I wanted a peer dependency to accept any prerelease, I've done something like:

"peerDependency": {
  "depName": "^1.0.0-0"
}

I'm not sure of the exact term for the trailing -0 of the semver, but I've considered it a wildcard, of sorts.


EDIT: To provide some more context. My experience is limited to doing this with NPM, but I would hope how various package managers handle semantic versioning would be the same. In the case of npm, it uses the semver module for evaluating semantic versions. Here's an rough example of how I think npm evaluates prerelease versions.

import semver from 'semver';

const accepted = '^1.0.0-0';
const prereleases = [
  '1.0.1-alpha.0',
  '1.0.6-beta.0',
  '1.0.6-next.0',
  '1.3.0-0.64-34-aerf3asdf33'
];

for(const prerelease of prereleases) {
  const satisified = semver.satisfies(prerelease, accepted, { includePrerelease: true });
  console.log('satisified', prerelease, satisified);
}
Related