简体   繁体   English

有没有办法清理 node_modules 目录,只留下所需的依赖文件?

[英]Is there a way of cleaning a node_modules directory leaving only the required dependent files?

Installing and managing packages using npm is great!使用 npm 安装和管理包很棒! What I don't find so great is the mess it can leave.我觉得不太好的是它可能留下的烂摊子。

I've started using npm for both client and node dependency management and I've noticed lots of different arrangements for the various packages I rely on.我已经开始将 npm 用于客户端和节点依赖项管理,并且我注意到我依赖的各种包有很多不同的安排 Some have lib folders, some have src folders, some dist , some docs , some examples Etc. I understand that this is because generally these packages come directly from source.有些有lib文件夹,有些有src文件夹,一些dist ,一些docs ,一些examples等。我理解这是因为通常这些包直接来自源代码。

My question is:我的问题是:

Is there a way of identifying or even automating the removal of any unneeded files, for deployment to production?有没有一种方法可以识别甚至自动删除任何不需要的文件,以部署到生产环境? I'm thinking: removal of any readme.md (easy enough I guess) or example files (probably easy enough).我在想:删除任何 readme.md(我猜很容易)或示例文件(可能很容易)。

Ideally I'd like to be able to calculate exactly what the dependency tree looks like from my entry point and delete unneeded / unused files ... This is obviously a lot harder for client packages that rely on images or fonts or HTML for example.理想情况下,我希望能够从我的入口点准确计算依赖树的样子并删除不需要/未使用的文件......这对于依赖图像或字体或 HTML 的客户端包显然要困难得多。

EDIT:编辑:

As pointed out by Alexander Mac (below) A good strategy for front-end dep's is to include client dependencies as dev-dependencies and build.正如 Alexander Mac(下文)所指出的,前端 dep 的一个好策略是将客户端依赖项包含为 dev-dependencies 和 build。 So my question is only related to nodejs apps.所以我的问题只与 nodejs 应用程序有关。

I might suggest building your code (and deps) into one bundle with rollup.js or webpack2 .我可能会建议使用rollup.jswebpack2将您的代码(和 deps)构建到一个包中。

These module loaders leverage tree-shaking approach to include only the code that is actually used.这些模块加载器利用摇树方法仅包含实际使用的代码。

You can run: npm prune which will remove modules from ./node_modules not specified as dependencies in packages.json.您可以运行: npm prune ,它将从 ./node_modules 中删除未在 packages.json 中指定为依赖项的模块。

npm dedupe will remove duplicate dependencies by pulling equivalent modules up to the root ./node_modules/. npm dedupe将通过将等效模块拉到根目录 ./node_modules/ 来删除重复的依赖项。 I don't know how useful this is though.我不知道这有多大用处。 I did it and then realised there were a whole bunch of modules I was using indirectly which I could also use directly, so there's that.我做到了,然后意识到我间接使用了一大堆模块,我也可以直接使用它们,所以就是这样。

A surefire way of doing this is also:这样做的一个可靠方法也是:

rm -rf ./node_modules ./bower_components npm install

It's good practice to do a clean build before release to production anyway.无论如何,在发布到生产环境之前进行干净的构建是一种很好的做法。

Also, I use sinopia to cache modules locally, which relieves network traffic, so these re-installs are a bit less time intensive.另外,我使用sinopia在本地缓存模块,这减轻了网络流量,因此这些重新安装的时间密集程度要低一些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM