Automatically Remove Unused Node Modules with Python

We keep a directory under the root of our projects called _dev_tools.

It consists of Python scripts we can quickly run, usually by pressing the Play button of the Code Runner extension in VS Code, to help with project management.

The key is creating…


This content originally appeared on DEV Community and was authored by Jeremy Gollehon

We keep a directory under the root of our projects called _dev_tools.

It consists of Python scripts we can quickly run, usually by pressing the Play button of the Code Runner extension in VS Code, to help with project management.

The key is creating tools that work cross-platform on Windows, Mac, and Linux.

Here's our script for removing unused node modules.

import json
from sys import platform
from subprocess import run

div = "=================================="
use_shell = platform == "win32"

print(f"\nFinding unused dependencies\n{div}\n")

cmd = ["npx", "depcheck", "--json"]
depcheck_result = run(cmd, shell=use_shell, capture_output=True, text=True)

unused_dependencies = json.loads(depcheck_result.stdout)["dependencies"]
if len(unused_dependencies) > 0:
    print(f"Found these unused dependencies\n{div}")
    print(*unused_dependencies, sep="\n")

    affirmative_responses = {"y", "yes", "Y", "YES", ""}
    response = input(f"{div}\n\nRemove all? [yes] ").lower() in affirmative_responses

    if response == True:
        cmd = ["yarn", "remove", *unused_dependencies]
        run(cmd, shell=use_shell)

    print(f"\nDone!\n{div}\n")

else:
    print(f"\nDone! - No unused dependencies found.\n{div}\n")


This content originally appeared on DEV Community and was authored by Jeremy Gollehon


Print Share Comment Cite Upload Translate Updates
APA

Jeremy Gollehon | Sciencx (2021-11-19T20:12:00+00:00) Automatically Remove Unused Node Modules with Python. Retrieved from https://www.scien.cx/2021/11/19/automatically-remove-unused-node-modules-with-python/

MLA
" » Automatically Remove Unused Node Modules with Python." Jeremy Gollehon | Sciencx - Friday November 19, 2021, https://www.scien.cx/2021/11/19/automatically-remove-unused-node-modules-with-python/
HARVARD
Jeremy Gollehon | Sciencx Friday November 19, 2021 » Automatically Remove Unused Node Modules with Python., viewed ,<https://www.scien.cx/2021/11/19/automatically-remove-unused-node-modules-with-python/>
VANCOUVER
Jeremy Gollehon | Sciencx - » Automatically Remove Unused Node Modules with Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/19/automatically-remove-unused-node-modules-with-python/
CHICAGO
" » Automatically Remove Unused Node Modules with Python." Jeremy Gollehon | Sciencx - Accessed . https://www.scien.cx/2021/11/19/automatically-remove-unused-node-modules-with-python/
IEEE
" » Automatically Remove Unused Node Modules with Python." Jeremy Gollehon | Sciencx [Online]. Available: https://www.scien.cx/2021/11/19/automatically-remove-unused-node-modules-with-python/. [Accessed: ]
rf:citation
» Automatically Remove Unused Node Modules with Python | Jeremy Gollehon | Sciencx | https://www.scien.cx/2021/11/19/automatically-remove-unused-node-modules-with-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.