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