2

How can I transpile Typescript without using the command line?

I want to have a script build.js that will build my application.

So far, I found that the typescript package has a transpile(inputArgument, compileOptions) method, but I couldn't find anything about this on their documentation.

I can have my project built using tsc -p server, so I imagined the following would work:

const tsc = require('typescript');

tsc.transpile("", {
    project: "server",
});

Then I'd use node build.js.

I couldn't get the tsc.transpile() to work (I couldn't figure out what the inputArgument was) and I couldn't find on their documentation how to do this: transpile from code, without the CLI.

1 Answer 1

2

You could use child_processmodule

In your case:

const { spawn } = require('child_process');

const ls = spawn('tsc', ['-p', 'server']);

ls.stdout.on('data', (data) => {
  console.log(`stdout: ${data}`);
});

ls.stderr.on('data', (data) => {
  console.log(`stderr: ${data}`);
});

ls.on('close', (code) => {
  console.log(`child process exited with code ${code}`);
});
Sign up to request clarification or add additional context in comments.

1 Comment

This is not what I was looking for, but it does solve the problem. Thank you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.