Browserify import/require?

panthro picture panthro · Oct 9, 2015 · Viewed 10.3k times · Source

I'm trying to pick up browserify and have been through a number of examples.

In one example I see the use of 'import':

import 'jquery';

and importing local files with:

import Header from './Header';

but in other examples I see people importing via:

require('./Header');

What is the difference?

Answer

JMM picture JMM · Oct 9, 2015

require() is the Node module system (CommonJS) in ES5. import is ES6 module syntax.

If you want to browserify modules written with ES6 module syntax you'll need to compile them using something like babelify (or babel by other means).