Ionic 2 - Generate RFC4122 UUID's

What is the right way to generate UUID’s with Ionic 2?

I looked at angular2-uuid, but the implementation is very naive and I don’t trust it to generate good UUID’s.

I have also tried to use node-uuid, but I’ve been unable to get that to work. I have a stackoverflow question about that.

I have tried node-uuid before too. But I ended up writing mine like so:

 var utilAPI={
	createGuid:function(){
		return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
			var r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
			return v.toString(16);
		});
	},
}

You can try that.

2 Likes

This I can fix. Disable builtins in the rollup configuration. See here for how to customize the rollup config, and provide one that just comments out the buildins() line.

@rapropos, thanks. I tried that, but unfortunately this doesn’t work.

rollup: Treating 'crypto' as external dependency
rollup: No name was provided for external module 'crypto' in options.globals – guessing 'crypto'

node-uuid relies on Crypto, but as far as I can tell disabling the builtins plugin means that transitive dependencies of npm modules are not loaded, which would explain why this fails. Right?

Oh, sorry about that. I needed crypto for something else, and didn’t realize it was also needed for node-uuid. Add this to your custom rollup config:


  external: [
    "crypto"
  ],

  globals: {
    crypto: "crypto"
  }