EternallyCurious
EternallyCurious

Reputation: 2415

Conflict between RequireJs and NodeJS definitions in Typescript

I got the Typescript RequireJS definition from Definitely Typed - which has an ambient declaration of Require that conflicts with the NodeJs command "require". Here's the declaration and the error:

Declaration:

declare var require: Require;

Error:

C:/.../require.d.ts(320,13): error TS2134: Subsequent variable declarations must have the same type.  Variable 'require' must be of type '{ resolve(id: string): string; cache: any; extensions: any; main: any; (id: string): any; }', but here has type 'Require'.

Upvotes: 6

Views: 3134

Answers (3)

SomeBruh
SomeBruh

Reputation: 460

I had this issue while trying to integrate monaco-editor in my application. Workaround is pretty simple.

  1. Insert this line <script>window.requirejs = window.require;</script> after included require.js or loader.js in your html.

  2. Then in your .ts file where you want to use requirejs, replace all require with requirejs. This alias is already defined in require.d.ts

  3. Done!

Upvotes: 1

dcsan
dcsan

Reputation: 12275

i was having this issue and removed this from the typings.json

"require": "registry:dt/require#2.1.20+20160316155526"

not sure how it got into my project, but i'm not using require client side.

Upvotes: -1

basarat
basarat

Reputation: 276191

That is true. What I had to do was simply use declare var require:any and not reference nodejs or RequireJS.

Would appreciate if you can open a bug here : https://github.com/borisyankov/DefinitelyTyped

Reason why it's tricky is because you don't actually have both node and RequireJS in the same environment. It's a compile time reuse thing we need to support.

Upvotes: 2

Related Questions