1

When you have a dependency that is going to install into current project or a NPM package, it is easy to determine if it is a 'deps' or a 'devDeps', but how to determine if it is a 'deps' or a 'peerDeps'?

Wayne Mao
  • 419
  • 5
  • 9

1 Answers1

1

I try to answer this question myself. See if someone agree or can correct me.

How to determine if a deps should be put into 'devDependencies' or 'peerDependencies'?

if (your source code /src/ depends on it) {
  if (you can't live without it) {
    if (you don't want to affect the version that consumer choose to install) {
      // This is usually for big or heavy deps, like framework level, for example: react, redux
      Put it into 'peerDependencies'
    } else {
      // This is regular case
      Put it into 'dependencies'
    }
  } else {
    // This is a special case, means it is an optional for you in just some of the use cases.
    // For example, [Swiper](https://github.com/nolimits4web/swiper), it supports react | vue | angular, but people use vue won't want to see a peerDeps of 'react', right?
    Do nothing
  }
} else if (only development environment depends on it) {
  // This is usually used for compile and test tools.
  Put it into 'devDependencies'
}

How to handle/install the local environment if the project has peerDeps?

if (you want to make package.json clear enough, without confusion) {
  Just Leave 'peerDeps' in 'peerDependencies'
  Then use 3rd party tool to install 'peerDeps' locally, without updating package.json.
  See https://github.com/helloint/peerdeps-test
} else {
  Put 'peerDeps' in 'devDependencies' as well as 'peerDependencies'
  This way, npm install will also install these 'peerDeps', just that they are not really 'devDeps'
}

Note: Top Level Project shouldn't have direct 'peerDeps'. 'peerDeps' is a relationship description that lower package to the upper package or project. For project, it is already the top, no need to tell.

Wayne Mao
  • 419
  • 5
  • 9