4

I am trying to utilize type definition file https://www.npmjs.com/package/diff-match-patch as described in the link (Angularjs).

Please Note: I am using Visual Studio as IDE

var DiffMatchPatch = require('diff-match-patch'); <- Error here
var dmp = new DiffMatchPatch();
app.constant("dmp",dmp);

However, I am getting this error:

Cannot find name 'require'

This is how the type definition file is structured:

declare module "diff-match-patch" {
    type Diff = [number, string];

    export class diff_match_patch {
        static new (): diff_match_patch;

        Diff_Timeout: number;
        Diff_EditCost: number;
        Match_Threshold: number;
        Match_Distance: number;
        Patch_DeleteThreshold: number;
        Patch_Margin: number;
        Match_MaxBits: number;

        diff_main(text1: string, text2: string, opt_checklines?: boolean, opt_deadline?: number): Diff[];
        diff_commonPrefix(text1: string, text2: string): number;
        diff_commonSuffix(text1: string, text2: string): number;
        diff_cleanupSemantic(diffs: Diff[]): void;
        diff_cleanupSemanticLossless(diffs: Diff[]): void;
        diff_cleanupEfficiency(diffs: Diff[]): void;
        diff_cleanupMerge(diffs: Diff[]): void;
        diff_xIndex(diffs: Diff[], loc: number): number;
        diff_prettyHtml(diffs: Diff[]): string;
        diff_text1(diffs: Diff[]): string;
        diff_text2(diffs: Diff[]): string;
        diff_levenshtein(diffs: Diff[]): number;
        diff_toDelta(diffs: Diff[]): string;
        diff_fromDelta(text1: string, delta: string): Diff[];
    }

    export var DIFF_DELETE: number;
    export var DIFF_INSERT: number;
    export var DIFF_EQUAL: number;
}
Shyamal Parikh
  • 2,988
  • 4
  • 37
  • 78
  • Possible duplicate of [typescript getting error TS2304: cannot find name ' require'](http://stackoverflow.com/questions/31173738/typescript-getting-error-ts2304-cannot-find-name-require) – David Sherret Oct 28 '15 at 21:46

1 Answers1

2

I am trying to utilize type definition file https://www.npmjs.com/package/diff-match-patch as described in the link (Angularjs).

If you are using the npm module file in front end, you need to use something like webpack and compile with --module commonjs.

Also use import/require instead of var/require. More : https://basarat.gitbooks.io/typescript/content/docs/project/modules.html

basarat
  • 261,912
  • 58
  • 460
  • 511
  • I am trying to utilize the file for back-end WebApi Visual Studio. This type definition file is very different from what I have used for example in moment.js it has `declare moment = moment.MomentStatic` and then for acessing the file in app.ts I used `app.constant("moment", moment);` I have no idea how I can make this work, would appreciate if you can elaborate. – Shyamal Parikh Oct 29 '15 at 07:01
  • Moreover, all definition files I utilize currently are interface based while this one is class based. – Shyamal Parikh Oct 29 '15 at 07:08