Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8645679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:36:40+00:00 2026-06-12T12:36:40+00:00

From within a node app I would like to do: var typeScript = require(‘typescript’);

  • 0

From within a node app I would like to do:

var typeScript = require('typescript'); 

typeScript.compile('...')

I’m looking to implement the compiler into a build system but without access to a public API (typescript.compile, etc) this is impossible.

Here’s a more complete example of what I would like to do, though the below is for LiveScript, not TypeScript, utilitized in a plugin written for the Brunch.io build-system:

LiveScript = require 'LiveScript'
sysPath = require 'path'

module.exports = class LiveScriptCompiler
  brunchPlugin: yes
  type: 'javascript'
  extension: 'ls'

  constructor: (@config) ->
    null

  compile: (data, path, callback) ->
    try
      result = LiveScript.compile data, bare: yes
    catch err
      error = err
    finally
      callback error, result

  include: [
    (sysPath.join __dirname, '..', 'vendor', 'prelude-browser-0.6.0.js')
  ]

Curious if anyone found a work-around?

Update

I ended up implementing my own solution to a variety of the problems listed above and elsewhere. Please see https://github.com/damassi/TypeScript-Watcher for more information and usage.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:36:43+00:00Added an answer on June 12, 2026 at 12:36 pm

    This one is a bit hacky but it will work.

    I thought about this very same just yesterday and I was checking their code. If you check bin/typscript.js from their sourcecode (It is a very very large file, with nearly 21k lines of code), you will see it creates TypeScript.TypeScriptCompiler, and then you will find that this DOES expose a way of compiling.

    var compiler = new TypeScript.TypeScriptCompiler(outfile, errorfile, 
        new TypeScript.NullLogger(), settings);
    

    Now, you need an easy way to expose it. To do this, you will have to modify their code, which is why this is hacky. To do this, you could modify typescript.js by adding:

    module.exports = exports = TypeScript;
    

    Right at the end of the file.

    Then, you can create an index.js file in the root of the module (notice: install the module in a local scope for all of this: “npm install typescript”), which exposes the object.

    exports.TypeScript = require("bin/typescript");
    

    And ready! Now you can just call it and compile your code using it. You can check how to use the API for compilation in the tsc.js file.

    I apologize in advance for the horrible code ahead:

    var fs = require("fs");
    var TypeScript = require("typescript");
    var path = "test.ts";
    var pathout = "test.js";
    var content = fs.readFileSync(path, "utf-8");
    var fd = fs.openSync(pathout, 'w'); 
    var outFile = { 
        Write: function (str) { 
            fs.writeSync(fd, str); 
        }, 
        WriteLine: function (str) {
        console.log(fd, str); 
            fs.writeSync(fd, str + '\r\n'); 
        }, 
        Close: function () { 
            fs.closeSync(fd); 
            fd = null; 
        } 
    };
    var createFile = function (path) { 
        function mkdirRecursiveSync(path) { 
            var stats = fs.statSync(path); 
            if(stats.isFile()) { 
                throw "\"" + path + "\" exists but isn't a directory."; 
            } else { 
                if(stats.isDirectory()) { 
                    return; 
                } else { 
                    mkdirRecursiveSync(_path.dirname(path)); 
                    fs.mkdirSync(path, 509); 
                } 
            } 
        } 
        mkdirRecursiveSync(_path.dirname(path));
        console.log(path) 
        var fd = fs.openSync(path, 'w'); 
        return { 
            Write: function (str) { 
                fs.writeSync(fd, str); 
            }, 
            WriteLine: function (str) { 
                fs.writeSync(fd, str + '\r\n'); 
            }, 
            Close: function () { 
                fs.closeSync(fd); 
                fd = null; 
            } 
        }; 
    };
    var stderr = { 
        Write: function (str) { 
            process.stderr.write(str); 
        }, 
        WriteLine: function (str) { 
            process.stderr.write(str + '\n'); 
        }, 
        Close: function () { 
        } 
    }
    var compiler = new TypeScript.TypeScriptCompiler(outFile, outFile);
    compiler.setErrorOutput(stderr);
    compiler.addUnit(content, path);
    compiler.typeCheck();
    compiler.emit(false, createFile);
    outFile.Close();
    

    For some reason whoever wrote the code was a real fan of C# and proceeded to go ahead and use methods called WriteLine, Close and Write, which are in fact just wrappers. You could get this of the overhead of having to add this functions, but you would have to modify a lot of code in the module and it isn’t worth it. I think it is best to have a class to extend (or if you are still on JS, inherit the prototype) and let it do that for you, to make it DRY.

    Something really nice is that if you want to translate 500 TypeScript files and put them all into a single .js file, you can just call compiler.addUnit(anothercontent, anotherpath); 500 times and then see it all go into a single file 🙂

    Focusing on better things: if you check tsc.js code, you will find a batch compiler class. If you want this for a build process, it might be better to use something more robust like it. It provides watching files and more.

    Having browsed the code, I think I will just submit a ticket to the development team and ask them to provide a clearer API ¬¬

    Note: All file reads in here are done in a synchronous way. This is bad, very bad, in terms of performance. I don’t know exactly what you plan to do, but I couldn’t recommend more that you find a way to make this async if possible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to essentially run a wget from within a node.js app? I'd
I try to refer with an xsd:keyref from within a node/subnode structure to a
My app crashes when I try to use the features of b2Body from within
I'd like to call a simple YUI3 function from within a JavaScript function. Here
I'm trying to integrate posting to one's wall from within my app.i try this
How can I make an HTTP request from within Node.js or Express.js? I need
I have a PhantomJS/CasperJS script which I'm running from within a node.js script using
I'm trying to access a particular native Windows API call from within a node.js
I've found plenty of documentation on how to perform Splunk searches from within Node.js
I'm working with node and would like to include a module stored on a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.