Search code examples
javascriptangulartypescripthashids

How do I use pure javascript library, hashids.js on angular?


I tried importing the library directly

import * as hash from '../../../../node_modules/hashids';

and tried this code

let id = hash.encode(this.studentDocument.student_id_number); console.log(id);

But it throws this error, sad.

_node_modules_hashids__WEBPACK_IMPORTED_MODULE_2__.encode is not a function

I even tried this

declare var hash:any;

But it throws this error

hash is not defined

Any tip would be greatly appreciated! (a cont. of this post)


Solution

  • You need to create a new instance of the hashids object.

    import * as hash from 'hashids';
    
    const hashids = new hash();
    const id = hashids.encode(348029348);
    console.log(id);