Logo

dev-resources.site

for different kinds of informations.

Automatically Remove Unused Node Modules with Python

Published at
11/19/2021
Categories
python
npm
npx
javascript
Author
gollyjer
Categories
4 categories in total
python
open
npm
open
npx
open
javascript
open
Author
8 person written this
gollyjer
open
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 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")
Enter fullscreen mode Exit fullscreen mode
npx Article's
30 articles in total
Favicon
Improving Port Management Speed: Why I Created `port-client` to Replace `npx kill-port`
Favicon
How to install react
Favicon
npm vs npx: Choosing the Right Tool for the Job
Favicon
npm vs npx - What's the difference?
Favicon
npmใจnpxใฎ้•ใ„
Favicon
Package Manager Fight: npm vs pnpm vs npx vs yarn vs bun
Favicon
App::cpx
Favicon
NPM vs NPX: What's the Difference?
Favicon
npm vs npx โ€” What are the Basic Difference?
Favicon
Create an NPX professional card
Favicon
Diferenรงa entre NPM INIT eย NPX
Favicon
Demystifying NPM and NPX: A Dive into Package Management
Favicon
npm vs npx: Friends or Enemy?
Favicon
Understanding npm vs npx: A Developer's Guide ๐Ÿ“ฆ๐Ÿš€
Favicon
Understanding NPM and NPX in frontend Development
Favicon
A NodeJS newbie's guide to understanding NPM and NPX
Favicon
Errors when creating strapi app using the command npx create-strapi-app@latest --quickstart
Favicon
Npx, c'est quoi ?
Favicon
You are running `create-react-app` 4.0.3, which is behind the latest release (5.0.0)
Favicon
What is NPX?
Favicon
Use this NPM script to create your EditorConfig files
Favicon
Creating an npx Command
Favicon
Automatically Remove Unused Node Modules with Python
Favicon
Animated CLI Profile Card
Favicon
Useful Npx Packages for the Developer's Everyday Life
Favicon
Short Video example in 20 Seconds. Get a Github directory quickly by a simple command without installation.
Favicon
My personal business card - What's next?
Favicon
Creating my personal business card
Favicon
When to Use Global NPM Installs? Rarely
Favicon
Useful NPX

Featured ones: