Search code examples
npmnpm-workspaces

List NPM workspaces similar to lerna ls


I'm looking for an NPM command similar to lerna ls that would print out all workspaces.

For example, let's say I have package1 and package2 in a packages sub-directory and my package.json looks like this:

"workspaces": [
 "./packages/*"
]

I would like to get a list of NPM 7 workspaces. For the example I would expect:

  • packages/package1
  • packages/package2

I was hoping npm ls -p --depth 0 would do this, but unfortunately it's printing out other dependencies as well.

I guess I could use npm ls -json and parse out the top level dependencies. However, I'm hoping there's a better way?


Solution

  • So for now I'm using JQ for this.

    Example: npm ls --production --depth 1 -json | jq -r '.dependencies[].resolved'

    For my example this results in:

    file:../../packages/package1
    file:../../packages/package2
    

    I don't know why it adds ../../ in front of it. So to further improve this: npm ls --production --depth 1 -json | jq -r '.dependencies[].resolved[11:]' returns the expected result:

    packages/package1
    packages/package2
    

    I've also submitted a feature request here: https://github.com/npm/cli/issues/4086