Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
93
rated 0 times [  100] [ 7]  / answers: 1 / hits: 34071  / 9 Years ago, mon, july 13, 2015, 12:00:00

I have a node.js library lib written in ES6 (compiled with Babel) in which I export the following submodules:



use strict;

import * as _config from './config';
import * as _db from './db';
import * as _storage from './storage';

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


If from my main project I include the library like this



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


I can see the proper output and it work as expected { config: ... }. However, if I try to include the library like this:



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


it will be undefined.



Can someone explain what is happening here? Aren't the two import methods supposed to be equivalent? If not, what difference am I missing?


More From » node.js

 Answers
24
import * as lib from 'lib';

is asking for an object with all of the named exports of 'lib'.




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

are named exports, which is why you end up with an object like you did.




import lib from 'lib';

is asking for the default export of lib.




e.g.


export default 4;

would make lib === 4. It does not fetch the named exports. To get an object from the default export, you'd have to explicitly do


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

[#65825] Friday, July 10, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ulysses

Total Points: 111
Total Questions: 118
Total Answers: 113

Location: Zambia
Member since Sat, Oct 31, 2020
4 Years ago
;