【问题标题】:How do I make "export default" compile to "module.exports"如何使“导出默认”编译为“module.exports”
【发布时间】:2020-06-23 00:52:51
【问题描述】:

我正在使用 typescript 编写,并将其设置为编译为 commonjs。每当我输入export default 时,它都会编译成exports.default 而不是module.exports。我正在制作一个 NPM 包,所以我希望解决这个问题。我该如何解决?如果需要,我可以添加我的tsconfig.json 文件。

【问题讨论】:

    标签: node.js typescript node-modules


    【解决方案1】:

    您可以在 TS 文件中简单地用module.exports 编写它

    const myExportedObj = {
    };
    
    module.exports = {
        foo: myExportedObj,
    };
    

    输出:

    var myExportedObj = {};
    module.exports = {
        foo: myExportedObj,
    };
    

    或者,据我从你的问题中可以看出,你可以不用export default,而是export

    TS

    export const myExportedObj = {
    };
    
    

    JS

    exports.myExportedObj = {};
    

    另一个好方法是创建一个index.ts 文件并从那里导出模块 TS

    export * as Context from './context';
    

    JS

    exports.Context = require("./context");
    

    【讨论】:

      猜你喜欢
      • 2020-06-07
      • 1970-01-01
      • 2019-05-22
      • 1970-01-01
      • 2019-04-25
      • 2013-01-24
      • 2020-04-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多