Deploying a node.js project without npm on the client side

I would like to deploy a nodejs project with frequent updates. npm is not available on the site, so I have to pack node_modules. This works fine, but it takes a long time to send to the client via an accessible ftp connection (80 MB of mostly node_module files each time). My workflow is as follows:

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 minimizes my code just for what is needed. The node_modules folder is copied to the assembly folder. If I use npm install --production , I get a smaller size, but skip the tools I need to create it first. Therefore, in the end, I will make some effort to make my code size small, but all my work has been canceled, because I need to pack such a large tree node_modules.

Is my approach wrong? Is there an easier way to deploy when npm is not available on the production server or is there a good way to reduce the size of the node_modules folder?

+5
source share
1 answer

Update : after writing this answer, npm3 (and yarn) and flattened npm dependencies appeared. This reduces the size of node_modules significantly (perhaps 20% - 30% for a typical project). However, some of the tips below reduce your footprint by an order of magnitude.

I have compiled a list of results for those who want

  • deployment without npm on the server or
  • reduce the size of the node_modules folder

Smaller node_modules size:

  • Use npm prune --production to remove devDependencies and clean add-ons

    In my case, this node_modules folder size is about 20%.

    Most of the large files in the node_modules folders node_modules limited to a small number of modules that are not used at run time! . Cleaning / deleting this data reduces the area by 10 times! for example: karma, gazebo, less and grumble. Many of them are used by the modules themselves and do not have a place in production. The disadvantage is that npm installation must be done before each build.

  • Use partial npm packages

    Many npm packages are available in parts. For example, from installing all async or lodash sets only the bits that you need: for example,

Bad: npm install -save lodash async

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

Typically, individual lodash modules make up the 1 / 100th full package size.

  • npm-package- minifier can be used to reduce the size of the node_modules tree

    Compact node_modules for client-side deployment

    This basically removes a lot of unused files in the node_modules tree. This tool will also reduce the size of devDependencies so that it works in a "production" version of node_modules.

Reducing the size of updates

  • Differential deployment

    As mentioned in the comments, updates can be divided into updates that require dependency changes or change only business logic. I tried this approach and it significantly reduces the area for most updates. However, it also increases deployment complexity.

+6
source

Source: https://habr.com/ru/post/1201398/


All Articles