Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm attempting to use a node module in a typescript application and I've included it in my TypeScript code via require().

I build my typescript to JS and all works well if I run it through node which is great.

I'm looking for a way to use the same code and run it through the browser. Browserify seemed like a sensible solution.

I popped a post build action on my VS project and I'm building a 206KB file via browserify (so it's certainly doing something!). Unfortunately my tiny bit of Typescript doesn't seem to be accessible when it's been browserified. I'm not that familiar with what browserify should be generating so not quite sure whether what it's wrapped my .js code in is correct (can post snippets if it helps).

So my question is twofold really, I'm looking for the answer to either:

  1. Is there a recommended way to write TypeScript post 0.9 to allow it to be run after it's been browserified?
  2. Is there a way to simple tell TypeScript to pull in the 'require' dependency on its own?

Any thoughts or info in this area would be greatly appreciated.

EDIT:

Just to clarify, I'm generating my .js from the .ts during save/build, and in a post build action, pointing browserify to the output. An abridged js output looks like this:

var TestModule;
(function (TestModule) {
    function launchDrone() {
        var exports = require('ar-drone');
        var client = exports.createClient();
    }
})(TestModule || (TestModule = {}));

When I generate the browserified file from that, I can't access TestModule or launchDrone in any context (or certainly not from window. ) is there some trick to accessing the browserified code?

share|improve this question
    
browserify puts all the referenced js files into one file. it won't render the js. you'll likely need to add a step to turn typescript into js before browserifying it. –  J.Wells May 8 '14 at 18:10
    
Yeah I'm doing that already, my .js file is generated and that's what I'm pointing browserify at –  dougajmcdonald May 8 '14 at 18:13
    
Ah - gotcha now. Are there any errors in the console? Have you tried prefixing your individual scripts with ;? –  J.Wells May 8 '14 at 18:18
    
I'm not getting any errors as far as I can see (VS is usually quite noisy, I'll run the post build command manually and let you know if I see anything, one tick) –  dougajmcdonald May 8 '14 at 18:25
    
Nope, no errors at all, here's what I'm using as a build action "browserify e:\nodejs\myproject\app.js -o e:\nodejs\myproject\bundledapp.js" should I be using any other params for browserify? I only pick that up from some online example –  dougajmcdonald May 8 '14 at 18:26

1 Answer 1

It looks like you potentially are not exporting TestModule? Your TestModule file should look like this:

module TestModule {
    export function launchDrone() {
        var exports = require('ar-drone');
        var client = exports.createClient();
    }
}
export = TestModule;

This way you should be able to launch TestModule and TestModule.launchDrone from the window.

share|improve this answer
    
Interesting, I'll have a look at this tonight. I believe by changing some of the module options I can get the export = TestModule line to generate, but this didn't seem to help when I did it. –  dougajmcdonald May 27 '14 at 11:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.