How do I make "export default" compile to "module.exports"

Viewed 982

I'm writing in typescript with it set to compile to commonjs. Whenever I put export default, it compiles into exports.default instead of module.exports. I am making an NPM package, so I want this to be fixed. How do I solve it? If needed, I can add my tsconfig.json file.

2 Answers

You can simply write it with module.exports in your TS file

const myExportedObj = {
};

module.exports = {
    foo: myExportedObj,
};

Output:

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

Or, as far as I can see from your question, you could don't export default but export instead

TS

export const myExportedObj = {
};

JS

exports.myExportedObj = {};

Another good approach is to make an index.ts file and export modules from there TS

export * as Context from './context';

JS

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