Binding to uuid-random


#1

I am trying to use uuid-random from npm. I have this in my packages.json:

  "dependencies": {
    "uuid-random": "^1.3.0"
  }

and this in my .re file:

[@bs.val] [@bs.module "uuid-random"] external uuid : unit => string = "uuid";
let testUuid = uuid();

at runtime, I get this error:

var testUuid = UuidRandom.uuid();
TypeError: UuidRandom.uuid is not a function

I am missing something obvious (to everyone except me). What is it?


#2

The docs have these two examples:

Babel

import uuid from 'uuid-random';
uuid(); // 'f32dc9ae-7ca8-44ca-8f25-f258f7331c55'

Node

var uuid = require('uuid-random');
uuid(); // '0b99b82f-62cf-4275-88b3-de039020f14e'

I’m not sure how to bind to the second one (the required value itself is the function), but it seems like it must export default to work with the babel approach. What about this binding?

[@bs.val] [@bs.module "uuid-random"] external uuid : unit => string = "default";
let testUuid = uuid();

#3

Alas, I get the same error with a different name:

node src/ReQTI.bs.js
/home/david/qti/reqti/src/ReQTI.bs.js:6
var testUuid = UuidRandom.default();
                                 ^

TypeError: UuidRandom.default is not a function

I might try a different UUID module and see if I can get it to work.

Edit: using uuid from npm, this works great:

[@bs.val] [@bs.module "uuid"] external uuidv4 : unit => string = "v4";
let testUuid = uuidv4();
Js.log(testUuid);

#4

The following code can be used to bind to a function which is the “default” export from a node module:

[@bs.module] external uuid: unit => string = "uuid-random";
let test = uuid()

For any module, you can leave the name out of the module annotation and use the module name in the binding name declaration section.

Playground example:


#5

Thanks! That worked great.