babel / babel-loader

📦 Babel loader for webpack
MIT License
4.82k stars 451 forks source link

`module.exports` is returned instead of `module.exports.default` when importing from node_modules #1010

Closed bchenSyd closed 10 months ago

bchenSyd commented 10 months ago

hi guys,

forgive me if I post the question in the wrong place, as I'm not too sure if it's a babel-loader issue, or typescript issue.

so basically I'm using a 3rd party library which is commonJS module with default export

// XX/index.js
'use strict';
Object.defineProperty(exports, '__esModule', { value: true }); 
exports.default = "this is the export string";

when I consume it directly from my project, which is Creat-react-app (typescript + babel-loader), below code works fine

import XX from 'xx';

console.log(XX); // XX is the exported string , i.e. it's    module.exports.default

However, I have another esm library/module yy, which depends on xx,and when I debug the code I noticed that inside node_modules/yy/index.js, imported XX is the module object , i.e. module.exports instead of module.exports.default. Is is the expect behaviour or is this a defect in babel/typescript?

// node_module/yy/index.js

import XX from 'xx';

console.log(XX);  // <-------------  XX is an object {__esModule: true, default: 'this is the exported strign'}
export const YY='something'

// src/index.js
import XX from 'xx'; //OK XX is `module.exports.default`
import {YY} from 'yy'; // Error: inside node_module/yy/index.js, XX is `module.exports`

can someone please shed some light on this ? thanks! (and sorry I've put the question in the wrong channel)

JLHwung commented 10 months ago

Duplicate of https://github.com/babel/babel/discussions/16112