我想部署一个经常更新的nodejs项目。 npm 在该站点不可用,因此我必须打包 node_modules。这工作正常,但需要很长时间才能通过可用的 ftp 连接发送给客户(每次 80MB 大部分是 node_module 文件)。我的工作流程如下所示:
git clone project
npm install # installs all my dev tools which I need for packaging
grunt build
tar xvzf build.tar.gz build/
构建步骤仅压缩我的代码打包所需的内容。 node_modules 文件夹被复制到构建文件夹中。如果我使用 npm install --production
,我的足迹会更小,但首先会错过构建它所需的工具。所以最后我做了一些努力来减少我的代码占用空间,但是我所有的工作都因为不得不打包这么大的 node_modules 树而付诸东流。
我的方法错了吗?在生产服务器上没有 npm 的情况下,是否有更简单的部署方式,或者是否有减少 node_modules 文件夹大小的好方法?
最佳答案
Update: Since writing this answer, npm3 (and yarn) arrived, and flattened npm dependencies. This reduces the size of the
node_modules
folder considerably (perhaps 20% - 30% for a typical project). Nevertheless, some of the tips below that will reduce your footprint by an order of magnitude.
我已经为任何想要的人编制了调查结果列表
- 在服务器上不使用 npm 进行部署 或
- 减少 node_modules 文件夹的占用空间
更小的 node_modules 足迹:
使用 npm prune --production 删除 devDependencies 并清除其他模块
在我的例子中,这个 node_modules 文件夹大小大约减少了 20%。
node_modules
文件夹下的大量大文件仅限于少数运行时未使用的模块!。清除/删除这些可将足迹减少 10 倍!例如:karma、bower、less 和 grunt。其中许多由模块本身使用,并且在 生产构建。缺点是 npm install 必须在每次构建之前运行。使用部分 npm 包
许多 npm 包都是分部分提供的。例如,的 安装所有的
async
或lodash
只安装你需要的部分 需要:例如
错误:npm install -save lodash async
很好:npm install --save async.waterfall async.parallel lodash.foreach
通常,单个 lodash 模块的大小是整个包的1/100。
npm-package-minifier 可用于减小 node_modules 树的大小
Compacting node_modules for client-side deployment
这基本上删除了很多未使用的文件 node_modules 树。该工具将减小 devDependencies 也因此它应该在“生产”上运行 node_modules 的版本。
减少更新的大小
差异部署
如评论中所述,更新可能会拆分为需要更改依赖项或仅更改业务逻辑的更新。我已经尝试过这种方法,它大大减少了大多数更新的占用空间。但是,这也增加了部署的复杂性。
关于javascript - 在客户端部署没有 npm 的 node.js 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25564719/