Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import "old" ES5 code in ES6

Tags:

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?

like image 359
grssnbchr Avatar asked Apr 18 '16 07:04

grssnbchr


People also ask

Can I use ES6 imports?

Importing can be done in various ways:Node js doesn't support ES6 import directly. If we try to use import for importing modules directly in node js it will throw out the error.

Does ES6 Import Export?

With the help of ES6, we can create modules in JavaScript. In a module, there can be classes, functions, variables, and objects as well. To make all these available in another file, we can use export and import. The export and import are the keywords used for exporting and importing one or more members in a module.

What is ES5 vs ES6?

ES5 is an abbreviation of ECMAScript 5 and also known as ECMAScript 2009. The sixth edition of the ECMAScript standard is ES6 or ECMAScript 6. It is also known as ECMAScript 2015. ES6 is a major enhancement in the JavaScript language that allows us to write programs for complex applications.

What is require () in JavaScript?

1) require() In NodeJS, require() is a built-in function to include external modules that exist in separate files. require() statement basically reads a JavaScript file, executes it, and then proceeds to return the export object.


1 Answers

You can use the webpack exports loader:

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

See the shiming modules overview for details

like image 149
jantimon Avatar answered Oct 12 '22 00:10

jantimon