首页 > 解决方案 > 在生成的 commonjs 包中需要时如何避免使用点表示法访问默认导出的类实例

问题描述

我在 TypeScript 文件中导出一个类实例,如下所示:

索引.ts

export { myClient } from './my-client.ts'

我的客户.ts

class myClient {
}

export const myClient =  new myClient();

然后我将其编译为 es5 格式的 commonjs,当我在节点中使用它时,我必须像这样访问它:

const myClient = require('myclient').myClient;

虽然这可行,但有没有一种方法可以让我在不需要使用.myClient符号的情况下要求和使用我的课程?

更新

我尝试在我的 TS 文件中更改我的类实例的导出:

索引.ts

export { default as myClient } from './my-client.ts'

我的客户.ts

class myClient {
}

const myClient =  new myClient();
export default myClient;

但是我仍然必须在我的节点应用程序中像这样访问:

const myClient = require('myclient').myClient;

标签: typescriptcommonjs

解决方案


推荐阅读