I want to import different module to my module in ecma script 6. For example:
import rest from 'rest';
export function client() {
// some logic
}
If I will change the import statement to classic:
var rest = require('rest');
everything is working fine. Any ideas?
I am not no expert but import
is similar to require
in many ways, but the key difference are:
you can import selective items using import
( guess this is close to python), but with require
, you export only a single module as a namespace, everything else is it's sub-modules.
second is, require
is more of of node.js thingy(though you can bring it into browser using browserify ),, but import
is now a native feature of ES6, i.e browsers that support ES6, import
would work
Example from lukehoban's es6features to re-enforce my first point:
// lib/math.js
export function sum(x, y) {
return x + y;
}
export var pi = 3.141593;
// app.js
import * as math from "lib/math";
alert("2π = " + math.sum(math.pi, math.pi));
// otherApp.js
import {sum, pi} from "lib/math";
alert("2π = " + sum(pi, pi));
//Some additional features include export default and export *:
// lib/mathplusplus.js
export * from "lib/math";
export var e = 2.71828182846;
export default function(x) {
return Math.log(x);
}
// app.js
import ln, {pi, e} from "lib/mathplusplus";
alert("2π = " + ln(e)*pi*2);
This is an answer for my question, but if you want to know how to import other files please refer to the answer given by user @mido or for example check this page: http://www.2ality.com/2014/09/es6-modules-final.html
So comment from @Felix King has directed me to the right answer. As Felix suggested, rest module doesn't have default export function so it should be imported like that:
import * as rest from 'rest';
So it depends from a module, how it is written. For example "mime" interceptor module which is included in rest can be included by:
import mime from 'rest/interceptor/mime';
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