Currently, when I create npm package for Angular 2, mainly as Angular 2 services, I use peerDependencies
as follow:
"peerDependencies": {
"@angular/core": "^2.0.0",
"rxjs": "5.0.0-beta.12",
"zone.js": "^0.6.6"
}
I don't use dependencies
because I don't want my package to pull them during npm install
. As I expect the target application is Angular 2.
And put all dependencies needed to compile my package in devDependencies
:
"devDependencies": {
"@angular/core": "^2.0.0",
"rxjs": "5.0.0-beta.12",
"zone.js": "^0.6.6",
"typescript": "*",
"typings": "*"
}
However, should I use dependencies
or peerDependencies
?
My main concern is for application using the package. Do they affect the build process of the consuming application?
Or am I doing it plain wrong and should switch back to dependencies
?
When to use peerDependencies? Peer dependencies really come into play when you're developing code that will be used by others, such as plugins and packages. If you're just working on a final product (i.e one that can't really be used inside another project), then you don't really have to worry about it.
A dependency is a library that a project needs to function effectively. DevDependencies are the packages a developer needs during development. A peer dependency specifies that our package is compatible with a particular version of an npm package.
When running npm install , it does not install the peer dependencies automatically even if they are in the package-lock. json file. Instead it modifies the package-lock. json file removing all the peer dependencies.
Peer Dependencies are used to specify that our package is compatible with a specific version of an npm package. Good examples are Angular and React. To add a Peer Dependency you actually need to manually modify your package.json file.
From https://nodejs.org/en/blog/npm/peer-dependencies/
Peer Dependencies
What we need is a way of expressing these "dependencies" between plugins and their host package. Some way of saying, "I only work when plugged in to version 1.2.x of my host package, so if you install me, be sure that it's alongside a compatible host." We call this relationship a peer dependency.
As I interpret it, peer dependencies are simply to tell the one who's using your package that it won't work if they do not install the peer dependencies.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With