Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm overhead - how to handle this?

When installing anything via npm, it downloads dozens of not needed files. Usually I am looking for a library final build, a *.min.js file or anything like that but the rest is useless.

How do you handle all these useless files? Do you remove them by hand or generate the final app with any build tool like gulp or grunt?

I'm quite confused as I have plenty of npm modules installed in my webapp and the folder size is about 50 megabytes but it could be 2mb only.

like image 958
Tukkan Avatar asked Apr 05 '16 10:04

Tukkan


2 Answers

npm install --production

Just doing an npm install brings in both development and runtime dependencies. You could also set the ENV to production globally for the server: npm config set production.

See this github issue. Note that this won't get you only the final minified build of everything, but will greatly reduce the bloat. For instance, a library might rely on babel-cli, babel-preset-es2015, and uglifyjs to be built (devDependency), but you don't need any of that if it also includes the transpiled minified file.

like image 111
Jared Smith Avatar answered Nov 04 '22 18:11

Jared Smith


Managing Packages

For front end non-development packages I prefer Bower. It maintains the minified and non-minified version of your packages.

Build Tool

Use either Gulp or Grunt. Gulp would be my tool of choice.

Gulp task that will greatly improve your code are:

  • minification of both css and js
  • optimization/compression of images
  • concatenation and caching to reduce the number of calls to the server
  • package versioning
  • automatic injection of project dependencies
  • automatic injection of external dependencies
  • static analysis of js and css
  • automatic builds on code changes
  • deployment
  • testing

Node

If you can, leave to node all your development tools and leave to bower all your release plugins. Most node packages that are used in released apps have a bower installation counterpart.

Edit


Don't delete anything from Node manually as you don't know which packages have other packages as dependencies. If you are afraid that you may have junk in there, use npm rimraf to delete the node_modules folder, and then run npm install. Most importantly check your package.json for unnecessary saved packages.

like image 42
Wilmer SH Avatar answered Nov 04 '22 18:11

Wilmer SH