When you have an ESM module like this:
// greetings.js
export const hello = 'hello';
export const hi = 'hi';
and you try to import it using default export:
import greatings from './greetings.js';
You will get The requested module './greetings.js' does not provide an export named 'default'
error, which is expected.
But for some packages, like yup
, I noticed you can import it like this:
import yup from 'yup';
if you install Yup and check index.esm.js
, you will notice, it also doesn’t have any default export. so how we are able to import it using default?
If you want to say, Yup is shipped with a CommonJS module (index.js
), I have to say yes, but some other packages like rotating-file-stream
are also shipped with a CommonJS module, but we get the does not provide an export named 'default'
error when we try to import it like using default import.
I trid these:
import yup from 'yup';
console.log(yup);
and
import rfs from 'rotating-file-stream';
console.log(rfs);
first one works, but second one doesn’t, why?!