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?
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.
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.
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.
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.
You can use the webpack exports loader:
var riveted = require("exports?riveted!riveted")
See the shiming modules overview for details
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With