ES6 Module Syntax Support with CommonJS

So, I have this simple module:

export default function(){} 

If I do not use export default , then the TypeScript compiler will write one warning that my “module has no default export”, which I would like to avoid.

So, to use this module, we would do:

 import fn from 'my-module'; 

that everything is good and good, but what if I want to use CommonJS to import it?

Then I have to do this:

 const fn = require('my-module').default; 

This is rather inconvenient for users. Is there any way around this?

+5
source share
1 answer

There is an equivalent:

 import tscmultiwatch from 'tsc-multi-watch'; const {default:tscmultiwatch} = require('tsc-multi-watch'); 

This way you can avoid the less pleasant:

 const tscmultiwatch = require('tsc-multi-watch').default; 

And tsc-multi-watch might look like

 export default function(){ } 
-2
source

Source: https://habr.com/ru/post/1264414/


All Articles