Vadorequest Vadorequest - 4 months ago 30
Javascript Question

Node module using requirejs and typescript

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?

Answer

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.

Comments