How to import "old" ES5 code in ES6
27,500
You can use the webpack exports loader:
var riveted = require("exports?riveted!riveted")
See the shiming modules overview for details
Related videos on Youtube
Author by
grssnbchr
Updated on September 21, 2020Comments
-
grssnbchr over 3 years
I have an
ES6
application (withBabel 6.5
andWebpack
) 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 variableriveted
, but it has an apparently validpackage.json
pointing toriveted.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?
-
thomaux about 8 yearsTry doing
import * as riveted from 'riveted'
-
slebetman about 8 yearsAre you trying to run this in a browser?
-
grssnbchr about 8 yearsYes @slebetman. @Anzeo thanks but this gives me a yet slightly different error
riveted.init is not a function
. By the way,riveted
is correctly "installed" intonode_modules/riveted
. -
CodingIntrigue about 8 years@wnstnsmth Looks like you need to use the exports-loader to shim the module as it's not properly exported
-
slebetman about 8 yearsThen just include riveted.js in a script tag before the rest of your script. There is no need to require. It is already browser code, not node.js code that you need to make work in a browser.
-
Isidro Martínez almost 8 yearsdid you tried with
import { default as es6Module } from 'es5Module';
?
-
-
gonzofish over 7 yearsthis was a HUGE help to me in a project. thank you so much.