When comparing npm + Browserify vs Webpack, the Slant community recommends npm + Browserify for most people. In the question“What are the best open source front-end package managers?” npm + Browserify is ranked 1st while Webpack is ranked 2nd. The most important reason people chose npm + Browserify is:
If you're using node.js as your backend, you gain a lot of flexibility by using the same package manager for the frontend and backend, making it much easier to share code without adding one more tool to an already large toolbase.
Ranked in these QuestionsQuestion Ranking
Pros
Pro Best way to share code with the backend
If you're using node.js as your backend, you gain a lot of flexibility by using the same package manager for the frontend and backend, making it much easier to share code without adding one more tool to an already large toolbase.
Pro Huge active ecosystem
Npm gains a lot from its large community, and the activity from node.js gives npm the largest set of active repositories. Since so many people already use npm, chances are the library you need has already been added to npm, and many new authors are writing their libraries with npm in mind.
Pro Client side shims provided by Browserify
Browserify provides client side versions of non I/O related built in npm modules. This allows you to use the path manipulation, crypto, and zip libraries on the client side.
Pro Support multiple bundles
Browserify allows splitting up bundles among multiple pages to get the benefit of caching for shared, infrequently-changing modules, while still being able to use require().
Pro All modules must implement CommonJS modules which leads to cleaner dependency management
Because all npm modules need to follow the CommonJS format, it's much easier to set up dependencies through the require function.
Pro Easy to create bundles
Because of the nature of Browserify, it's easy to require
different dependencies and concatenate them into one minified file.
This helps with performance and load times, especially for mobile devices.
Pro Can also flatten the dependency tree with dedupe
npm dedupe lets you flatten the dependency tree.
Npm has a very handy dedupe tool. What this tool does is that it checks the dependency tree to find modules that depend on third dependencies. If a suitable version for all modules exists, it keeps that version and deletes any other versions that are not needed.
For example, in this dependency graph:
a
+-- b <-- depends on c@1.0.x
| -- c@1.0.3
-- d <-- depends on c@~1.0.9
-- c@1.0.10
dedupe will transform it to:
a
+-- b
+-- d
-- c@1.0.10
What it did was to make sure that both b
and d
got their dependency met by a single c
module. It then deleted all the unneeded versions of the c
module.
Pro Browserify shim allows you to use non Common JS formatted packages
Browserify shim is a transformation extension for Browserify that lets you load in libraries that do not follow the Common JS structure (using an exports module). This allows you to explicitly define what globally defined variables should be exposed by a require
statement so you can control how you load in these poorly formatted libraries safely without polluting the global scope.
Because the Common JS style loads in required libraries within a closure, any variables defined in a library will not pollute the global scope. Browserify shim lets you define which variables defined within that closure to map to the exports
variable that Common JS expects in a safe and explicit way through declarations in your project's package.json
file.
Pro Can include HTML, CSS and images as well
Packages hosted with NPM do not include just JavaScript. Other assets, like static ones (HTML, CSS and images) are included.
Pro Rich and flexible plugin infrastructure
Plugins and loaders are easy to write and allow you to control each step of the build, from loading and compiling CoffeeScript, LESS and JADE files to smart post processing and asset manifest building.
Pro Tap into npm's huge module ecosystem
Using Webpack opens you up to npm, that has over 80k modules of which a great amount work both client-side and server-side. And the list is growing rapidly.
Pro Can create a single bundle or multiple chunks loaded on demand, to reduce initial loading time
Webpack allows you to split your codebase into multiple chunks. Chunks are loaded on demand. This reduces the initial loading time.
Pro Supports source maps for easier debugging
Source maps allow for easier debugging, because they allow you to find the problems within the origin files instead of the output file.
Pro ES6 module support
Webpack supports ES6 modules and their import
and export
methods without having to compile them to CommonJS require
Pro Share the same modules client-side and server-side
Because Webpack allows you to use the same require() function as node.js, you can easily share modules between the client-side and server-side.
Pro Bundles CommonJs and AMD modules (even combined)
Webpack supports AMD and CommonJS module styles. It performs clever static analysis on the AST of your code. It even has an evaluation engine to evaluate simple expressions. This allows you to support most existing libraries.
Pro Mix ES6 AMD and CommonJS
Webpack supports using all three module types, even in the same file.
Pro Limit plugin integration issues
Cons
Con Familiarity with Node is required
Browserify's documentation assumes that you have some familiarity with Node before starting to work with it. Some methods are not explained very thoroughly and others are assumed to be already known by the developer.
For example, Node's module.exports
is not explained, it's just mentioned that browserify modules can be exported using it. Which is fine, but for a developer not used with Node, or a front-end engineer that has never used Node it can be confusing.
Con Does not guarantee reproducible builds
Con Needs to create a bundle in every change
That makes debugging more difficult and requires extra time to create the bundle again for every change.
Con Post-install scripts
Packages shouldn't need post-installing. They should be an enclosed unit. This is another potential attack vector.
Con Potentially hijacked and malicious packages
Con Config file may be hard to understand
Due to a somewhat hard to grasp syntax, configuring Webpack may take some time.