Should rxjs used in an npm lib be a dependency, peerdependency, or both?

Viewed 232

I have gone through many many posts and articles on dependencies vs peerDependencies, but still not 100% sure to do in my very simple situation....

I have a library (published to a private npm repo) that uses rxjs, eg it has some functions with Observable return types (I consume these in other Angular projects). So, is has a source file that has the following import...

import { Observable, Subscriber, TimeoutError } from 'rxjs';

So, I would normally think that this is a dependency, I need rxjs to be able to build it.

However, any consuming apps, I would think we would want the exact rxjs version. Reading this article, it seems if I have 'rxjs' as a dependency in my project (eg lets say v6), I would also want the consuming project to also be using 'rxjs' v6 (and not say v7). But if I only have rxjs as a dependency, wouldn't npm just install another "private" version of rxjs under my libraries node_modules?

But if I include rxjs under just peerDependencies, I cannot build, I get...

error TS2307: Cannot find module 'rxjs'.

   1 import { Observable, Subscriber, TimeoutError } from 'rxjs';
                                                           ~~~~~

I am using rxjs as a simple concrete example here, but this could happen for other libraries as well.

Does this mean I need it as BOTH a dependency AND a peerDependency? I see nowhere else that mentions this?

1 Answers

You should put the dependency in devDependencies and peerDependencies.

devDependencies will provide it during development, but not in a production build.

Related