DEV Community

Carl Saunders
Carl Saunders

Posted on

Add Destroy Script πŸ”₯πŸ’» To package.json

On rare occasions I find myself having to clean my development environment (i.e. delete the following folders - node_modules, build, coverage, etc). As this could include multiple folders, I decided the best approach was to create a script. I also hate typing the same commands over and over again, save those fingers!πŸ–οΈ

I wanted a cross platform approach (works on both Linux and Windows), rimraf is the ideal candidate as it's an npm package that deletes folders recursively and works cross platform.

Why make the solution cross platform? So all developers that collaborate on the code base can benefit.

Now we could install rimraf globally, but we can't guarantee that another developer has this installed, which will result in the script failing when executed.

A cleaner approach is to have the dependency installed as a dev dependency. However, this will cause issues as rimraf will try to delete the node_modules folder where rimraf is currently executing from. Hence it would try and delete itself and fail.

So to resolve this issue, we can execute rimraf when we need to run it (one-off invocation without local installation) via the npx command. The npx command was introduced with npm 5.2.0 and later.

The Destroy Script πŸ”₯

Below is an example of the destroy script, this will delete the node_modules, build and coverage folders and all their contents.

{
  "scripts": {
    "destroy": "npx rimraf node_modules build coverage",
  }
}
Enter fullscreen mode Exit fullscreen mode

Execute Destroy Script

The destroy script can be executed either by using npm or yarn.

npm run destroy
Enter fullscreen mode Exit fullscreen mode

Or

yarn destroy
Enter fullscreen mode Exit fullscreen mode

Top comments (15)

Collapse
 
elabftw profile image
eLabFTW

Instead of "destroy" you should use "clean" like in "make clean" ;) Destroy sounds too harshful imho!

Collapse
 
deadlybyte profile image
Carl Saunders

I usually have a "clean" script that deletes just the build artifacts. Hence the reasoning behind the name "destroy", as hopefully devs will know it applies to destroying all artifacts and dependencies.

Collapse
 
elabftw profile image
eLabFTW

then go with "cleanall", as destroy implies removing something that cannot be recuperated easily. My 2 cents ;)

Collapse
 
subztep profile image
Andras Serfozo

Nice one! I was using a bash file for it but this looks better, thanks. :)

Collapse
 
deadlybyte profile image
Carl Saunders

No worries, better to have it as a script in the package.json, then it's not dependant on the OS and it's also version controlled.

Collapse
 
subztep profile image
Andras Serfozo

Except the script doesn't work on Windows.

Thread Thread
 
projectmagenta profile image
Project-Magenta

it does. ive tested it

Thread Thread
 
subztep profile image
Andras Serfozo

Yeah that's what I meant, bash is not working with win cli. I also updated:) Cheers

Collapse
 
abdelrahmanahmed profile image
Wahdan

Nice one πŸ™Œ

Collapse
 
deadlybyte profile image
Carl Saunders

No worries, glad you liked it.

Collapse
 
valentinprgnd profile image
Valentin Prugnaud 🦊

I like that! πŸ”₯

Collapse
 
adahyto profile image
Adam

Thanks for sharing! I like this idea.

Collapse
 
deadlybyte profile image
Carl Saunders

No worries, glad you found it useful.

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt

I thought that in package.json, you don't need to put in npx.

Collapse
 
deadlybyte profile image
Carl Saunders

This would only work if you had rimraf as a dependency or rimraf was installed globally.