TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Show HN: Node.js node_modules file pruning

111 pointsby tjholowaychukover 7 years ago

17 comments

orfover 7 years ago
Rather than two giant 440px high images consisting of tiny text in the readme I&#x27;d quite love to read exactly what it does and why. Does it prune files in modules that are not needed? If so what heuristics does it use to prune files? Or does it prune modules that are not needed?<p>Reading the code[1] it appears to delete directories and extensions based on a blacklist.<p>1. <a href="https:&#x2F;&#x2F;github.com&#x2F;tj&#x2F;node-prune&#x2F;blob&#x2F;master&#x2F;prune.go#L15-L51" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;tj&#x2F;node-prune&#x2F;blob&#x2F;master&#x2F;prune.go#L15-L5...</a>
评论 #15732222 未加载
gcommerover 7 years ago
Love it. Saved 600MB... and test cases still passed :)<p>I ported it to bash really quick, as thats probably easier for people to drop into their dotfiles if they don&#x27;t have a Golang env setup.<p><a href="https:&#x2F;&#x2F;gist.github.com&#x2F;gpittarelli&#x2F;64d1e9b7c1a4af762ec467b1c7571dc2" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;gpittarelli&#x2F;64d1e9b7c1a4af762ec467b1...</a><p>If we all just do our part and send a PR setting up .npmignore for one or two of these projects, maybe we won&#x27;t need this anymore
评论 #15732895 未加载
评论 #15732854 未加载
TheCorehover 7 years ago
<a href="https:&#x2F;&#x2F;github.com&#x2F;tj&#x2F;node-prune&#x2F;blob&#x2F;master&#x2F;prune.go#L57" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;tj&#x2F;node-prune&#x2F;blob&#x2F;master&#x2F;prune.go#L57</a><p>Doesn&#x27;t this also result in .d.ts files being removed? These are type declaration files (Kinda like C&#x27;s .h files) that provide types for you without the file size overhead of the full TypeScript source.
评论 #15732827 未加载
mambodogover 7 years ago
The Yarn autoclean command also does something like this: <a href="https:&#x2F;&#x2F;yarnpkg.com&#x2F;lang&#x2F;en&#x2F;docs&#x2F;cli&#x2F;autoclean&#x2F;" rel="nofollow">https:&#x2F;&#x2F;yarnpkg.com&#x2F;lang&#x2F;en&#x2F;docs&#x2F;cli&#x2F;autoclean&#x2F;</a>
评论 #15732847 未加载
lxeover 7 years ago
Awesome idea. Especially if you’re packaging your projects via something like Docker this can shave hundreds of megabytes. (Even if you’re using the Docker layer cache, any change to your dependencies will bust the entire node_modules cache.)<p>The caveats is that this won’t work 100% of the time. There will be a dependency deep down in the tree that needs to read a markdown file from the file system or something of that sort. These issues could be hard to debug if this tool doesn’t provide good visibility into what’s being deleted.
评论 #15732990 未加载
namuolover 7 years ago
Is this serious?<p>I mean, kudos for helping push node module developers to make better use of `.npmignore` et al, but this project strikes me as overly snarky or even trolling...<p>Seems like what we really need is for some volunteers to tackle the most-installed packages on npm that publish with many extraneous files.<p>Hell, you could even automate it by writing a bot that (politely) points out likely-extraneous files and opens a pull request with changes to `.npmignore` to clean things up... Hmm... Weekend project brewing...
评论 #15732776 未加载
sergiotapiaover 7 years ago
Shouldn&#x27;t this be handled entirely by npm? If these files are unnecessary why do they get downloaded in the first place?
评论 #15732928 未加载
评论 #15732288 未加载
评论 #15732330 未加载
评论 #15734837 未加载
评论 #15734410 未加载
finchiskoover 7 years ago
No disrespect to TJ (he did many awesome projects I use almost daily), but wouldn&#x27;t simple: &#x27;find node_modules -not -regex &quot;.*.[js,json]&quot; -delete&#x27;<p>do the same job? Why project in go?
评论 #15732885 未加载
billmalarkyover 7 years ago
Oh wow TJ is working in nodeland again. I thought he had moved entirely towards Go.<p>Glad to see more of his work!<p>Edit: It&#x27;s Go lol.
tomchentwover 7 years ago
Even better: submit a PR auto fix these issues on the corresponding GitHub repo when pruning
billmalarkyover 7 years ago
TJ, <a href="https:&#x2F;&#x2F;apex.sh&#x2F;" rel="nofollow">https:&#x2F;&#x2F;apex.sh&#x2F;</a> is so beautiful. Did you design it entirely yourself or did you get help and if so who helped? Also mind if I ask what your inspirations were?
评论 #15732742 未加载
franciscopover 7 years ago
I&#x27;ve been thinking about this in a different way: what about bundling things with rollup.js? Then each package in require() would be just a package.json and an index.js. I think this might even help with performance.<p>Edit: lightened up (;
twhbover 7 years ago
Symptomatic of the npm community’s overloading of “npm install” to serve both users and contributors. It’s easy to configure “npm publish” to do essentially this for all users, but it’s considered bad practice. Which I don’t understand, since the dev use case is still fulfilled - and better - by “git clone”. And merging the two doesn’t scale - Chrome’s dev download is several GB and takes hours to build.
vkjvover 7 years ago
We&#x27;ve started publishing modules with a whitelist in the npmignore to help combat this.<p><pre><code> * !dist&#x2F;**&#x2F;*</code></pre>
dstrootover 7 years ago
Nice job TJ. This definitely scratches an itch. Thought you were a Go dev these days. ;)
评论 #15732314 未加载
评论 #15732512 未加载
andreineculauover 7 years ago
Usually I don&#x27;t care about what language an executable is written in. After all, as a user I&#x27;m just interested in whether it executes or not.<p>But these small-software situations amaze me. Someone with a node_modules problem will have readily available sh, node and maybe python. So why golang? What could those not do, or golang can do better to such extent that it trumps availability? Similarly there&#x27;s a price to pay in terms of people contributing with a fix: who is interested in pruning node_modules and will send in a golang PR?<p>In other words, if a dev would prefer Java (specifically chosen because it exacerbates the startup time), would it still pass as ok? Luckily golang can compile to binaries but that implies you give up availability on the other end, now being confined to someone compiling and publishing on a regular basis, as opposed to just pushing a fix commit to a git url.<p>None of the above would be of importance if this would be a personal-quality repo with a note: hey i did this at 2am out of frustration, i chose the tools that i knew best, use it at your own risk, opensourced to share knowledge and to access it from my own projects, not as a &quot;productified&quot; software.<p>EDIT: I would much prefer a commenter&#x27;s solution in sh for the reasons above but also readibility: <a href="https:&#x2F;&#x2F;gist.github.com&#x2F;gpittarelli&#x2F;64d1e9b7c1a4af762ec467b1c7571dc2" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;gpittarelli&#x2F;64d1e9b7c1a4af762ec467b1...</a> :clap:
评论 #15732886 未加载
hiphipjorgeover 7 years ago
This is completely useless because npm still downloads those files and the hdd space is pretty negligeble. It should def go under the category of troll driven development. This might be useful for docker images that will be distrubuted and will be downloaded a lot, but even there it&#x27;s a stretch.<p>That being said, it would be great if npm had some functionality around packaging only the necessary files for actually running the module and removing all unnecessary files (tests, source code, documentation) and have an opt-in option to install those.
评论 #15732385 未加载
评论 #15732387 未加载