Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When creating Angular 2 library npm package, should I use peerDependencies or dependencies for Angular 2 dependencies?

Tags:

npm

angular

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?

like image 858
John Siu Avatar asked Oct 08 '16 00:10

John Siu


People also ask

When should I use peerDependencies?

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.

What is the difference between peerDependencies and dependencies?

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.

Does npm install peerDependencies?

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.

What is peerDependencies in angular?

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.


1 Answers

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.

like image 122
Gubagu Avatar answered Oct 13 '22 18:10

Gubagu