Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I use requirejs and typescript for a node_module.

https://github.com/Ayolan/validator-extended/blob/master/app.js

It works but I cannot load it once installed from npm. It looks like the requirejs config on the node_module change the config of my project (baseUrl and nodeRequire config actually).

There is a way to use TS and requirejs on a node_module?

share|improve this question
    
Why are you using RequireJS in a Node.js module? Node.js has its own module loading – Anzeo Jan 20 '14 at 13:57
    
To load typescript files. (AMD) – Vadorequest Jan 21 '14 at 8:14
    
You can compile TypeScript files to the CommonJS spec (used by node). Pass in the --module "commonjs" to the compiler – Anzeo Jan 21 '14 at 8:22
    
But... I know that. But you can't require them with the basic node.js require() function, you need something like requirejs. – Vadorequest Jan 21 '14 at 9:25
1  
Ok I'm glad I could help, I'll consolidate this as an answer. Could you please accept so that this question is closed? – Anzeo Jan 21 '14 at 10:37
up vote 3 down vote accepted

Node.js does not use the AMD specification for JavaScript modules but instead the CommonJS modules. You'll need to tell the TypeScript compiler to compile your modules to this specification. This can be easily achieved by passing in the --module "commonjs"flag to the compiler.

Please note, that although the CommonJS spec uses the require keyword, this is something completely different than RequireJS. Node.js does not rely on RequireJS for it's module loading, it has its own module loader that is based on the CommonJS spec.

In short: try to avoid using RequireJS and node.js.

share|improve this answer

His question makes sense. Using requirejs in node has a number of advantages over the standard commonjs: http://requirejs.org/docs/node.html

How to build node modules with AMD / Requirejs: http://requirejs.org/docs/node.html#nodeModules

share|improve this answer
    
I agree. I prefer starting my files with define and using dependency injection for Node over the traditional, CommonJS approach. It personally has the advantage of keeping my code clean and files small and simple. – Steve Hynding Aug 3 at 7:36

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.