Removing the node_modules folder

Problem:

I created a yoman project in error on my window window. Through explorer, when I try to delete it, I get a message that the path is too long.

Source too long error

A few solutions:

But is there a script based solution?

+6
source share
5 answers

You can write powershell with this effect, relying on npm

 PS C:\code\yeoman-foo> ls node_modules | foreach { >> echo $("Deleting module..." + $_.Name) >> & npm rm $_.Name >> } >> 

After executing the above command, you can delete the folder in the traditional ways ...

Go to the parent folder containing the project folder, select it and SHIFT + DEL

+3
source

You can use rimraf :

 npm install -g rimraf rimraf C:\code\yeoman-foo 
+10
source

You must use a power switch. This script recursively deletes any node_modules folder using PowerShell 3.

 :> ls node_modules -Recurse -Directory | foreach { rm $_ -Recurse -Force } 
+1
source

You can use node and javascript to remove the node_modules folder quite easily.

  • npm install --save-dev del
  • create a file in the root of your project called cleanup.js
  • use the node file system to iterate over the directory you want to delete, and del to delete files and folders.

 var fs = require('fs'), dir = (process.argv[2] ? process.argv[2] : 'node_modules'), colors = require('colors'), del = require('del'); (function cleanup(dir) { fs.readdir(dir, function doneReading(err, files) { if (err) { return err; } for (var i = 0; i <= files.length; i++) { if (typeof files[i] === 'string' || files[i] instanceof String) { if (files[i] !== 'del') { dir = dir + '/' + files[i]; if (!fileType(dir)) { return cleanup(dir); } } } } }); })(dir); function fileType(fileName) { console.log('Deleting: ' + fileName.yellow); if (fileName) { fs.stat(fileName, function(err, stat) { if (stat && stat.isFile()) { if (del(dir)) { var success = '| ---------------------------- FINISHING UP... ---------------------------- |'; console.log(success.green); } } else if (stat && stat.isDirectory()) { return false; } else { return err; } }); } } 

If this is too much, I made a script to help clean up the node_modules folder:

https://github.com/studio174/node-cleanup

0
source

The easiest way I've found so far (without installing or separate programs) is to simply run these commands in the root directory of your project (next to the node_modules folder):

 mkdir temp_dir robocopy temp_dir node_modules /s /mir rmdir temp_dir rmdir node_modules 

For convenience, you can also put this code in a .bat file and put it in the root directory of the project and run it when you want to delete the entire map node_modules

0
source

Source: https://habr.com/ru/post/986118/


All Articles