Search code examples
javascriptecmascript-6webpackes6-module-loader

How to import "old" ES5 code in ES6


I have an ES6 application (with Babel 6.5 and Webpack) and it successfully imports my modules like this:

import $ from 'jquery';

I wanted to install https://github.com/robflaherty/riveted/blob/master/riveted.js (a plugin for Google Analytics), but as you can see, the code doesn't have something like module.exports = ..., it only defines a global variable riveted, but it has an apparently valid package.json pointing to riveted.js.

So doing something like

import riveted from 'riveted'
riveted.init();

throws an error:

_riveted2.default.init is not a function

import riveted from 'riveted'
riveted.init();
import 'riveted'
riveted.init();

throws an error:

riveted is not defined

import * as riveted from 'riveted'
riveted.init();

throws an error:

riveted.init is not a function

How can I access riveted's init() function?


Solution

  • You can use the webpack exports loader:

    var riveted = require("exports?riveted!riveted")
    

    See the shiming modules overview for details