2
votes

How do I create node modules in typescript to be imported in other typescript files? I have searched and searched but cannot find how to do this properly.

I want to create a typescript module that exports some functions that I can import using require into my app.ts. I am using 'typescript-require'. Here is what I am doing (simplified):

app.ts

require('typescript-require');
import config = require('./config');

console.log(config.GetDefaultConfiguration());

config.ts

module config {
    var defaultConfig = "default configuration";
    export function GetDefaultConfiguration() {
        return defaultConfig;
    }
}

config.d.ts

declare module config {

}

But I get "TypeError: Object # has no method 'GetDefaultConfiguration'".

What am I missing?

1
I know this is a pretty old question, but for anyone else stumbling across it (as I did this past week), you might find this typescript boilerplate project helpful: github.com/bitjson/typescript-starter - Jason Dreyzehner

1 Answers

1
votes

I am using 'typescript-require'

Don't use it. Just compile to JS from TSC and then run the JS.

I get "TypeError: Object # has no method 'GetDefaultConfiguration'".

You need to export from config.ts:

var defaultConfig = "default configuration";
export function GetDefaultConfiguration() {
     return defaultConfig;
}

Note: don't use internal modules with external modules. If you are unclear about the difference. Watch this : https://www.youtube.com/watch?v=KDrWLMUY0R0&hd=1