简体   繁体   English

在客户端部署没有npm的node.js项目

[英]Deploying node.js project without npm on client side

I would like to deploy a nodejs project with frequent updates. 我想部署一个频繁更新的nodejs项目。 npm is not available at the site so I must package the node_modules. npm在站点上不可用,所以我必须打包node_modules。 This works ok but takes a long time to send to the customer over the available ftp connection (80MB of mostly node_module files every time). 这可以正常工作,但需要很长时间才能通过可用的ftp连接发送给客户(每次80MB主要是node_module文件)。 My workflow looks like this: 我的工作流程如下:

git clone project
npm install   # installs all my dev tools which I need for packaging
grunt build 
tar xvzf build.tar.gz build/

The build step minfifies my code packaging only what is needed. 构建步骤仅将我的代码打包缩小到需要的范围。 The node_modules folder is copied into the build folder. node_modules文件夹将复制到构建文件夹中。 If I use npm install --production , I get a smaller footprint but miss the tools I need to build it in the first place. 如果我使用npm install --production ,我会占用更小的空间但却错过了我需要的工具。 So in the end I go to some effort to make my code footprint small but all my work is undone by having to package such a large node_modules tree. 所以最后我努力使我的代码占用空间很小,但是我必须打包这么大的node_modules树来解决我的所有工作。

Is my approach wrong? 我的方法有误吗? Is there a simpler way to deploy where npm is not available on the production server or is there a good way to reduce the size of the node_modules folder? 是否有一种更简单的方法来部署生产服务器上没有npm的地方,或者是否有一种减小node_modules文件夹大小的好方法?

Update : Since writing this answer, npm3 (and yarn ) arrived, and flattened npm dependencies. 更新 :自写这个答案以来, npm3 (和yarn )到了,并且扁平化了npm依赖项。 This reduces the size of the node_modules folder considerably (perhaps 20% - 30% for a typical project). 这大大减小了node_modules文件夹的大小(对于典型项目,可能是20% - 30%)。 Nevertheless, some of the tips below that will reduce your footprint by an order of magnitude. 尽管如此,下面的一些提示可以将您的足迹减少一个数量级。

I have compiled the list of findings for anyone wanting to 我已经为任何想要的人编制了调查结果列表

  1. deploy without npm on the server or 在服务器上没有npm部署
  2. reduce the footprint of the node_modules folder 减少node_modules文件夹的占用空间

Smaller node_modules footprint: 较小的node_modules占用空间:

  • Use npm prune --production to remove devDependencies and purge additional modules 使用npm prune --production删除devDependencies并清除其他模块

    In my case this node_modules folder size by about 20%. 在我的情况下,这个node_modules文件夹大小约20%。

    The bulk of large files under node_modules folders is confined to a small number of modules that are unused at runtime! node_modules文件夹下的大量文件仅限于少量在运行时未使用模块! . Purging/deleting these reduces the footprint by a factor of 10! 清除/删除这些可将占地面积减少10倍! eg: karma, bower, less and grunt. 例如:业力,凉亭,少和咕噜声。 Many of these are used by the modules themselves and have no place in a production build. 其中许多都是模块本身使用的,并且在生产构建中没有位置。 The drawback is that npm install has to be run before each build. 缺点是必须在每次构建之前运行npm install。

  • Use partial npm packages 使用部分npm包

    Many npm packages are available in parts. 许多npm包都有部分可用。 For example, of installing all of async or lodash install only the bits you need: eg 例如,安装所有asynclodash只安装你需要的位:例如

Bad: npm install -save lodash async

Good: npm install --save async.waterfall async.parallel lodash.foreach

Typically, individual lodash modules are 1/100th the size of the full package. 通常,单个lodash模块的大小是整个包装的1/100

  • npm-package-minifier may be used to reduce the size of the node_modules tree npm-package-minifier可用于减小node_modules树的大小

    Compacting node_modules for client-side deployment 压缩node_modules以进行客户端部署

    This basically deletes a lot of unused files in the node_modules tree. 这基本上删除了node_modules树中的许多未使用的文件。 This tool will reduce the size of devDependencies also so it should be run on a 'production' version of node_modules. 此工具还将减少devDependencies的大小,因此它应该在node_modules的“生产”版本上运行。

Reducing size of updates 减少更新的大小

  • Differential deployment 差异部署

    As mentioned in the comments, updates may be split into updates where dependency changes are required or only business logic changes. 如评论中所述,更新可能会拆分为需要依赖项更改或仅更改业务逻辑的更新。 I have tried this approach and it greatly reduces the footprint for most updates. 我尝试过这种方法,它大大减少了大多数更新的占用空间。 However, it also increases the complexity of deployment. 但是,它也增加了部署的复杂性。

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

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