callum
callum

Reputation: 37769

Installing an npm module as a devDependency of itself

Let's say I've made a TDD tool called foo, and I want to use foo v1 to help me develop foo v2.

But when I npm install --save-dev foo@^1.0.0, npm says "Refusing to install foo as a dependency of itself".

  1. Why does npm refuse to do this?
  2. What can I do instead?

Workarounds I've got so far (and why they're not good enough):

Upvotes: 1

Views: 944

Answers (1)

Leonid Beschastny
Leonid Beschastny

Reputation: 51480

Here is a better alternative to your Workaround 2.


Let's assume that you'll only need this dependency on early stage of development. So, before publishing first production-ready version you'll get rid of it, e.g. by adopting mocha solution (using current stable version to test itself).

In this case, instead of publishing duplicate package you could temporary rename your package (i.e. postfix it with -dev).

To guarantee that this renamed package will not be published, you could also add private flag.

So, your dev package.json will look something like:

{
  "name": "mytdd-dev",
  "version": "2.0.0-dev",
  "private": true,
  ...
  "devDependencies": {
    "mytdd": "1.x.x",
    ...
  },
  ...
}

Then, when your package will be ready for the first release, you'll remove all -dev postfixes, private flag and dev dependency on previous version.

The only problem with this solution is that you won't be able to publish early dev versions of your TDD tool to npm (as long as you'll depend on previous version).


if installing an exact clone would work, then what would be the harm in npm allowing me to install [an old version of] foo as a devDependency of foo

I think it's a safety precautions against circular dependencies.

If you believe that npm should make an exception for devDependencies here, which sounds reasonable to me, then you should post your suggestion to npm issues tracker.

Upvotes: 2

Related Questions