Vadorequest
Vadorequest

Reputation: 17997

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?

Upvotes: 2

Views: 2162

Answers (3)

dekajoo
dekajoo

Reputation: 2102

If you don't need requirejs AND node at the same time That was my case when I had 2 separated typescript builds, one for the front and one for the back but I had only one package.json to put all the "@types" in so tsc would pick both .d.ts in node_modules/@types

You can just manually tell tscto take certain types with the type option:

types: [ "requirejs" ] for the front and types: [ "node" ] for the back

Hope this helps.

Upvotes: 2

FF_Dev
FF_Dev

Reputation: 578

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

Upvotes: 2

thomaux
thomaux

Reputation: 19718

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.

Upvotes: 2

Related Questions