javascripthtmlhashids

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!


Solution

  • 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.