0

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)

5
  • 1
    have you try to replace import * as hash from '../../../../node_modules/hashids'; by require('hashids') ? Commented Sep 10, 2018 at 7:23
  • Something like this, sir? require('hashids'); Commented Sep 10, 2018 at 7:28
  • How can I access the methods? Thank you for replying! Commented Sep 10, 2018 at 7:31
  • 1
    after require, just put declare var hashids:any; then consume as documentation. Commented Sep 10, 2018 at 7:44
  • It throws this error, ERROR ReferenceError: hashids is not defined. I guess it really has its quirks. Thanks for replying again sir! Commented Sep 10, 2018 at 7:47

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Sir wingnod, I threw the code to the constructor, minus the import but this error pops up. Cannot use 'new' with an expression whose type lacks a call or construct signature.
It didn't with import * as hash from 'hashids'; But worked with import * as hash from '../../../../node_modules/hashids/dist/hashids.min.js';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.