I use typescript to make NPM module and I write modules like export default xyz;
but I’d like TSC to translate it to CommonJS on transpilation.
And I want to keep const
and other ES6, just need the exports to be Node…
I’ve tried many TSCONFIG, as advised in some topics, currently it looks like
JavaScript
x
20
20
1
{
2
"compilerOptions": {
3
"incremental": true,
4
"target": "ES6",
5
"moduleResolution": "Node",
6
"esModuleInterop": true,
7
"declaration": true,
8
"declarationMap": true,
9
"removeComments": true,
10
"forceConsistentCasingInFileNames": true,
11
"strict": true,
12
"sourceRoot": "",
13
"outDir": "dist"
14
},
15
"include": [
16
"./src/main.ts"
17
],
18
"exclude": []
19
}
20
but it still produces JS file with export default xyz
instead of module.exports = xyz
.
How do I make it work?
Advertisement
Answer
Define in your config which module code should be generated by the typescript compiler.
JavaScript
1
6
1
{
2
3
"module": "CommonJS",
4
5
}
6