how to delete all node_modules from all packages in npm 7 workspace monorepo
Asked Answered
B

5

10

how can I delete all node_modules folders from all packages in an npm 7 workspace?

With Lerna we could just execute lerna clean -y but when migrating to native NPM 7+ workspaces, is there some equivalent?

Bund answered 19/11, 2021 at 6:22 Comment(0)
B
20

you can execute this to do so:

npm exec --workspaces -- npx rimraf node_modules && npx rimraf node_modules

explanation:

  • npm exec will execute whatever comes next
  • --workspaces will execute it in all your packages in your monorepo
  • -- means "here comes the command to execute"
  • npx rimraf node_modules is the command that's executed in all packages: it means it will delete the node_modules folder
  • && means "and then"
  • npx rimraf node_modules is executed again so the root folder node_modules also get deleted

That's all! Good luck

Bund answered 19/11, 2021 at 6:22 Comment(1)
Same for pnpm: pnpm -r exec rm -rf node_modulesNealneala
D
6

From this video on How to delete node modules like a pro.

npx npkill
Db answered 19/11, 2021 at 6:25 Comment(0)
G
4

I found this to work on version 10.2.3, without needing third party libraries.

npm exec --workspaces -c 'rm -rf node_modules'
Genethlialogy answered 5/1 at 2:44 Comment(0)
B
0

You can create a custom script in package.json like this:

package.json

{
  "name": "***",
  "description": "***",
  "scripts": {
     ...
    "cleandep": "rm -rf node_modules && pnpm -r exec rm -rf node_modules"
  },
  ...
}

Then run pnpm cleandep to remove all the node_modules inside the monorepo. I believe you can do the same with any other package manager

Binding answered 11/6 at 9:41 Comment(0)
A
-2

In lerna@7 Run the below command as mentioned here!

npx lerna clean -y
Antipodes answered 17/7, 2023 at 10:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.