Louay Al-osh
Louay Al-osh

Reputation: 3405

How to use "yarn" or "npm" to list specific type of dependencies

While developing a package, we need a way to know if package "x" exist in peer or dev dependencies, I'm trying

npm ls --omit dev # to list peers deps

and tried

yarn list --dev # to list dev deps

I didn't succeed in any of them.

Can someone help to solve this with a simple npm or yarn command?

Upvotes: 3

Views: 2216

Answers (2)

Trott
Trott

Reputation: 70075

If all else fails, you can write a Node.js script that does require('./package.json') and inspect the resulting object's dependencies, peerDependencies, and devDependencies values.

This won't tell you if they're actually installed yet or not, and it won't tell you about transitive dependencies.

npm offers ways to omit dev and peer dependencies from npm ls but not production dependencies, and I imagine that might be the issue you're running into. This is a clunky workaround for that.

Once you've done this to get a list of peer and dev dependencies, you can use child_process to run npm ls on each to see if they are actually installed.

This is not an elegant solution, but if nothing else is working, it should at least work.

> require('./package.json').devDependencies
{
  '@semantic-release/changelog': '^6.0.0',
  '@semantic-release/git': '^10.0.0',
  chai: '^4.2.0',
  karma: '^6.0.2',
  'karma-chai': '^0.1.0',
  'karma-chrome-launcher': '^3.1.0',
  'karma-coverage': '^2.0.3',
  'karma-firefox-launcher': '^2.0.0',
  'karma-ie-launcher': '^1.0.0',
  'karma-jasmine': '^4.0.0',
  'karma-mocha': '^2.0.1',
  mocha: '^9.0.0',
  nyc: '^15.0.1',
  requirejs: '^2.3.6',
  'semantic-release': '^18.0.0',
  standard: '^16.0.0'
}
> Object.keys(require('./package.json').devDependencies)
[
  '@semantic-release/changelog',
  '@semantic-release/git',
  'chai',
  'karma',
  'karma-chai',
  'karma-chrome-launcher',
  'karma-coverage',
  'karma-firefox-launcher',
  'karma-ie-launcher',
  'karma-jasmine',
  'karma-mocha',
  'mocha',
  'nyc',
  'requirejs',
  'semantic-release',
  'standard'
]
>

Upvotes: 0

Transformer
Transformer

Reputation: 7439

I believe your project file is not configured/missing the dependencies when you added them. Regardless, there are two steps to this, create the dependency and then; move it to peerDependencies listed below.

Also please share your project file for future questions

First you need to create those dependencies in your project (explained below #2)

Second/Then NPM/Yarn can leverage that and give the correct results when you list.


Step 1: How to create those dependencies

npm i @angular/core

  • This will add a property in your project file

"dependencies": {
    "@angular/core": "^12.0.0"
}

  • Now, after installing, move the installed package name to peerDependencies section.
"peerDependencies": {
    "@angular/core": "^12.0.0"
}

Step 2: How to list peer dependency, please replace react-native with your package

// for peer dependencies on a package & specific release
npm info react-native@latest peerDependencies

// for peer dependencies on a package *** replace react-native with your package
npm info react-native peerDependencies

// for production dependencies 
npm list -dev -depth 0

// or with flags
npm list -depth 0 -prod true

Upvotes: 1

Related Questions