javascript - Import always undefined when installing self-authored library through npm -
i authoring small js library first time , have problems getting imports work when using library.
the library structure this:
. ├── src │ ├── element.js └── index.js
element.js
function element() { console.log('i element'); } export default element;
index.js
export { element } './src/element';
now when installing library through npm
, importing it, import undefined
. this:
import { element } 'my-lib'; console.log(element); // undefined
i guess there error somewhere, can't find it! can spot error?
your error can solved 1 of 2 ways; changing element.js
or changing other 2 scripts:
element.js
:
... export element;
in case, named imports correctly reference function.
index.js
:
export element './src/element'; ...
import element 'my-lib';
in case, you're exporting/importing default namespace of element.js
rather named one.
the relevant documentation syntax can found @ mdn import
, mdn export
, , if may offer advice, should write test script confirms library working properly, , attach package.json
this:
"scripts": { "prepublish": "npm test", "test": "gulp test", ... }
or want execute it, whether mocha
, etc. doing prevent library publishing if 1 of tests in test script fails.
Comments
Post a Comment