We kind of introduced it end of last year in version 7.20, and it's a command to help you set and retrieve keys, values from your package.json files. So it is not only useful in the context of workspaces, but it's super useful when it's used in the context of workspaces.
So it can be used to, let me highlight here real quick. If you run like npm pkg get, it can be just a simple package. It's going to basically print out the content of package.json. And you can also filter out the properties, let's say, name, and then I get the name of my package there.
So when you bring on the support to the workspaces properties, it can actually get really powerful. Like in this example here, I'm going to retrieve the name of the version of all of my workspaces. So this config option here, dash dash WS, it's basically a way of saying, okay, target all of my configured workspaces. So if I run that, you can see it return the name of the version of each of my workspaces. And they're even keyed by the workspace name. So it can be really useful.
And also to highlight a little bit more of how they can be useful, let me set also some data into these package.json files. So let's say I'm managing this project and it's an open-source project. I just want to surface info about how users can fund my work. So I can go ahead and use npm pkg set. And let's say, I'm gonna set a founding key to all of these package.json. Then I'm gonna point it to my GitHub sponsors URL and I'm going to target all of the configured workspaces. So I can run that. And if you look up, let's say my usersync package.json, you can see that the founding info is there now. So same thing goes to my web app package.json. So it can be an incredible powerful way of just help you manage all of that data in your package.json across all of the configured workspaces.
So also highlight here that npm version, npm publish also have support to workspaces. So if you want to cut out, let's say, a new patch version of a workspace, that's possible today. And something to keep in mind is, as you can see here, I bumped my version to v1.0.1. I can look up my package.json there and see that the version is there, but one thing to keep in mind that has a bit different now from the way npm version works by default is that there's not a lot of committing and tagging when it's running npm version. So it's something to keep in mind, but you can definitely be looking forward to improvements to npm version specifically.
So to kind of wrap up everything, I wanted to run a quick example here. I'm using npm exec. It's basically npx.
Comments