2

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 Commented Feb 15, 2017 at 1:01

1 Answer 1

1

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

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

3 Comments

Thanks Basarat. Sounds weird but I tried that earlier and got compile errors. I am building my application in VS2013 using the node.js tools for Visual Studio and the build system has been pretty erratic. I did a clean build and with your suggested changes its working now.
I use grunt github.com/TypeStrong/grunt-ts simply because the Visual Studio compile pipeline for TypeScript is suboptimal. Note that the TypeScript team itself uses jake (not msbuild) to manage its compile
Thanks for the pointer. I will take a look at grunt and jake.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.