Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between browserify/requirejs modules and ES6 modules [closed]

I'm still new to ES6 and module loaders and I'm currently looking at combining a browserify setup with ES6. I was wondering if I would still need browserify/requirejs if I'm using ES6 modules. Seems like both allow you to define modules and export them? What is the difference between browserify/requirejs modules and ES6 modules?

like image 795
joerideg Avatar asked Feb 23 '15 13:02

joerideg


People also ask

Does Browserify support ES6?

TL;DR: Browserify lets you use NodeJS-like requires in your code. If you want to use ES6 modules, you'll need Babel/Babelify to convert your ES6 code into ES5, such that Browserify can understand it.

What is the difference between RequireJS CommonJS and AMD loaders?

AMD is more suited for the browser, because it supports asynchronous loading of module dependencies. RequireJS is an implementation of AMD, while at the same time trying to keep the spirit of CommonJS (mainly in the module identifiers).

What is the difference between CommonJS and ES6?

While CommonJS and ES6 modules share similar syntax, they work in fundamentally different ways: ES6 modules are pre-parsed in order to resolve further imports before code is executed. CommonJS modules load dependencies on demand while executing the code.

How are ES modules different from CommonJS modules?

The ES module usage format is the official standard to write JavaScript for maximum reusability and is what most web browsers natively support. However, NodeJS supports the commonjs module format by default, which are loaded using require() function, and the variables and functions are exported with the help of module.


1 Answers

After playing around for a while I did get a better understanding of things, also thanks to @Andy for the blog by Addy Osmani.

There are different module systems: AMD (RequireJS), CommonJS (Node) and the new ES6 module syntax (and the old ES5 Global system of course).

However if you want to use those in your browser you still need to load and wire those modules with some module loader library because browsers still do not support that. For that you could use a module loader like RequireJS, Browserify, SystemJS or es6-module-loader.

SystemJS is my personal favorite because it allows you to load any module system (AMD, CommonJS, ES6) and even use them interchangably in 1 app.

Update: In the mean time Webpack has become available and should be considered as a module loader as well.

like image 114
joerideg Avatar answered Oct 11 '22 17:10

joerideg