5

I developed an npm module (let's call it module-x) that I regularly update and publish on the npm platform.

Software that uses this module installs it by running npm i module-x but in dependencies section of the package.json the module-x is installed with a fixed version (module-x: "1.0.0").

I'd like to use the tilde or caret version instead resulting in something like this "module-x" : "~1.0.0".

I've seen that other libraries like request can do this, but how they do it?

  • The default for npm is to use semver versioning, so `^1.0.0` should be what your getting. Check your `.npmrc` make sure you haven't set `save-exact=true` to override this behaviour. – Keith Feb 16 '20 at 07:54
  • Hello @Keith, thank you for the reponse. I was using 0.0.x and it seems it isn't supported by npm and it must start from 1.0.0 (as described [here](https://docs.npmjs.com/about-semantic-versioning) ). Now it's using caret versioning. – Claudio Petrini Feb 17 '20 at 07:45

1 Answers1

0

The prefix in the package.json file (whether ^ or ~ or none) depends on the settings of npm on the machine, which installs the package, not on your settings of the project. From your example, you, as the creator of module-x, can't enforce the users of your dependency to have it installed either module-x: "^1.0.0" or module-x: "~1.0.0". That's their choice how to install it, hence how to update it.

You could:

  1. Ask them to manually add a prefix to your dependency
  2. Ask them to change the global settings of npm command with npm config set save-prefix "~" (this one will result in all their installations having ~ prefix)
Vladyslav Zavalykhatko
  • 15,202
  • 8
  • 65
  • 100