javascript - Difference between import X and import * as X in node.js (ES6 / Babel)? -


i have node.js library lib written in es6 (compiled babel) in export following submodules:

"use strict";  import * _config './config'; import * _db './db'; import * _storage './storage';  export var config = _config; export var db = _db; export var storage = _storage; 

if main project include library this

import * lib 'lib'; console.log(lib); 

i can see proper output , work expected { config: ... }. however, if try include library this:

import lib 'lib'; console.log(lib); 

it undefined.

can explain happening here? aren't 2 import methods supposed equivalent? if not, difference missing?

import * lib 'lib'; 

is asking object of named exports of 'lib'.

export var config = _config; export var db = _db; export var storage = _storage; 

are named exports, why end object did.

import lib 'lib'; 

is asking default export of lib. e.g.

export default 4; 

would make lib === 4. not fetch named exports. object default export, you'd have explicitly do

export default {   config: _config,   db: _db,   storage: _storage }; 

Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -