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:

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

  • There's a new way in npm@8:

    npm query .workspace | jq -r '.[].location'
    

    Returns the expected result:

    packages/package1
    packages/package2
    

    Still requires jq, but slightly cleaner than before.