Search code examples
javascriptnode.jstypescriptcode-translation

Transpile TypeScript In Memory With Node


Is there a way to transpile TypeScript in memory with Node? I would like to be able to get the generated JavaScript in memory.


Solution

  • Yes. TypeScript provides a a ts.transpileModule function:

    const ts = require('typescript');
    const source = "let x: string  = 'hello world'";
    const result = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS }});
    console.log(result.outputText); // var x = 'hello world';
    

    More