5

I am trying to run the TypeScript compiler from my Java application. To start, I am trying to figure out, whether I can run the compiler from command-line without Node.js:

$ jsc tsc.js

But this way I don't get any errors, nor help.

$ jsc tsc.js myscript.ts

Will get me nowhere.

It is easy to run js code directly from java (and I am hoping to run the compiler in this way), but is it possible to run TypeScript compiler without node.js?

EDIT:

I confirm the same behaviour with rhino.

0

4 Answers 4

5

I have a project, Typescript4j that does precisely this.

It runs the Typescript compiler wrapped within Rhino.

I'm using it successfully within Bakehouse, and a non-trivial Typescript application.

Sign up to request clarification or add additional context in comments.

1 Comment

No activity in the las two years, seems to be unmaintained :-(
3

Looking at the source code, the tsc command invokes a JS script tsc.js, which has 2 backends: Node.js and Windows Scripting Host. If any other JavaScript server supports reading and writing to a file system (like Rhino with RingoJS), it should be able to run the TypeScript compiler tsc.js.

Moreover, there is a fork of TypeScript compiler which claims to directly run on Rhino. So you could invoke Rhino directly from Java, without installing node.js.

1 Comment

Let's say I have Rhino installed, how do I install the Typescript module? Do I need to have npm installed as if I was using Node.js?
0

what you want to do is jsc node_modules/typescript/lib/tsc.js file1.ts, but unfortunately that won't work with engines different than node.js.

What will work (or at least works in the browser), is using TypeScript Compiler API, instead of trying to use the CLI (you will have to program). In the browser, you do this by loading the file node_modules/typescript/typescript.js and then you have access to the compiler API (https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API) via the global ts. Here you have an example of how to transpile a ts string to js using the compiler API: https://typescript-api-playground.glitch.me/#example=Transpiling-a-single-file

Good luck

Comments

-1

The TypeScript compiler is implemented in TypeScript, and can be used in any JavaScript host.

You may need to specify the full path to tsc.js

2 Comments

Their I/O code is only implemented for Node.js and Windows Scripting Host actually. Potentially it may run on various engines, but not without a bit of tinkering I suppose.
You may need to tweak it, but only if the I/O implementation was different - but that isn't equivalent to "it wouldn't run on another host" - you would pick one and see what needed to change.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.