最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - running js libraries that use require in typescript + bunJS forces the use of .default - Stack Overflow

programmeradmin8浏览0评论

I am trying to integrate an existing library in my new codebase using typescript and Bun. i am facing an issue where this library is requiring modules in code asynchronously, like follows :

let target = require(AppRoot + targetName);
        target.execute(targetTask);

so passing in the targetName, it can require the module which is a default exported object (usually an instantiated class). This was working fine when i was using this library in JS but when switching to Typescript the target became an Object with default being the actual instantiated class :

// in Javascript
Target {
  execute : [Function]
}
// in Typescript
Target {
  default : {
    execute : [Funtion]
  }
}

This change breaks the entire library of course as it relies on reading files that way. I am not suer if this is a Bun or Typescript issue, but is there a way to bypass forcing the default when importing for a specific library

Note: I already have esModuleInterop set to true in my tsconfig which according to chatgpt is the only hand-off solution

I am trying to integrate an existing library in my new codebase using typescript and Bun. i am facing an issue where this library is requiring modules in code asynchronously, like follows :

let target = require(AppRoot + targetName);
        target.execute(targetTask);

so passing in the targetName, it can require the module which is a default exported object (usually an instantiated class). This was working fine when i was using this library in JS but when switching to Typescript the target became an Object with default being the actual instantiated class :

// in Javascript
Target {
  execute : [Function]
}
// in Typescript
Target {
  default : {
    execute : [Funtion]
  }
}

This change breaks the entire library of course as it relies on reading files that way. I am not suer if this is a Bun or Typescript issue, but is there a way to bypass forcing the default when importing for a specific library

Note: I already have esModuleInterop set to true in my tsconfig which according to chatgpt is the only hand-off solution

Share Improve this question asked Apr 1 at 0:49 Kaki Master Of TimeKaki Master Of Time 1,6613 gold badges22 silver badges50 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

If you are using Bun just do

bun build --target=node file.js --outfile.js

Or, if you want, --target=bun.

Or, just execute the TypeScript .ts file directly with bun file.ts.

Done.

No tsconfig.json necessary because Bun doesn't parse Microsoft TypeScript with tsc.

发布评论

评论列表(0)

  1. 暂无评论