Reputation: 434
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?
Upvotes: 9
Views: 7700
Reputation: 2052
Here is a faster, pure-Node way to do it:
npm query .workspace | node -e "console.log(JSON.parse(require('fs').readFileSync(0)).map(w=>w.location).join('\n'))"
In my case, I wanted to find the paths to all the node_modules directories for each workspace, which just requires a slight adjustment to the output:
npm query .workspace | node -e "console.log(JSON.parse(require('fs').readFileSync(0)).map(w=>w.path + '/node_modules').join('\n'))"
Upvotes: 0
Reputation: 1
here is a bash version:
npm pkg get name -ws | grep -o '"@[_0-9a-z-]*/[_0-9a-z-]*":' | tr -d "'\":"
Upvotes: -1
Reputation: 101
Just in case anyone is looking for a pure NodeJS-npm solution:
npm exec -ws -c 'node -e "console.log(require(\"./package.json\").name)"'
Upvotes: 0
Reputation: 1503
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.
Upvotes: 15
Reputation: 434
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
Upvotes: 2