Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript. Uncaught TypeError: Hashids is not a constructor

Running the code from the tutorial at https://www.npmjs.com/package/hashids

<script type="text/javascript" src="js/hashids.min.js"></script>
<script type="text/javascript">

    var hashids = new Hashids();
    console.log(hashids.encode(1));

</script>

get the error: Uncaught TypeError: Hashids is not a constructor.

I am running this in a vanilla js app with no modules. just html and js files. If I use reqire(hasids) then it pops up with error: require is not defined. I need to run this on my app that is coded in regular browser javascript. Please help me run this. Thank you!

like image 652
Siddharth Agrawal Avatar asked Jun 06 '26 02:06

Siddharth Agrawal


1 Answers

That module's web information is incorrect. When you import the script the way you're doing it, you have to use

var hashids = new Hashids.default();

You might want to log a Github issue about it; it's clearly misleading as they explicitly tell you to do exactly what your code does.

like image 178
Pointy Avatar answered Jun 08 '26 14:06

Pointy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!