1

I'm building an Ionic 2 (RC0) application and I'm trying to use node-uuid by following the official documentation.

I've done:

$ npm install --save node-uuid
$ npm install --save @types/node-uuid

node-uuid seems to be using the default export approach, so I'm importing it in my typescript file like this:

import uuid from 'node-uuid';

And using it as follows:

console.log(uuid.v4);

However, my app doesn't come up and I see this error in the logs:

TypeError: des$3 is undefined

What am I missing?


Most resources for Angular 2 recommend using the typings CLI to install the type definitions, but this made no difference for me. I tried:

$ npm install --global typings
$ typings install --save node-uuid

$ ionic info

Your system information:

Cordova CLI: You have been opted out of telemetry. To change this, run: cordova telemetry on.
6.3.1

Ionic Framework Version: 2.0.0-rc.0
Ionic CLI Version: 2.1.0
Ionic App Lib Version: 2.1.0-beta.1
OS: Distributor ID: Ubuntu Description: Ubuntu 16.04.1 LTS 
Node Version: v6.6.0

******************************************************
 Dependency warning - for the CLI to run correctly,      
 it is highly recommended to install/upgrade the following:     
 Please install your Cordova CLI to version  >=4.2.0 `npm install -g cordova`
******************************************************
Niel de Wet
  • 7,806
  • 9
  • 63
  • 100

2 Answers2

2

Please note that node-uuid is deprecated. They merged with another project and now it's only called uuid. Everything up to the point of installing the @types library is correct. (note that you will have to redo those steps using just 'uuid' not 'nod-uuid')

However,

    console.log(uuid.v4);

does not generate an id. As per the documentation you need to specify in your import which version of uuid you want to use and then call your variable as a method: uuid();

From docs: [Deprecation warning: The use of require('uuid') is deprecated and will not be supported after version 3.x of this module. Instead, use require('uuid/[v1|v3|v4|v5]') as shown in the examples below.]

Here is a code example using uuid/v1:

    import { Component } from '@angular/core';
    import uuid  from 'uuid/v1'; //here change 'v1' with the version you desire to use

    @Component({
     selector: "page-uuid",
     templateUrl: "uuid.html"
    })

    export class uuidTestPage {
     id = uuid();

     constructor() {
       console.log(this.id); // outputs id. For example: 298da400-1267-11e8-a6e5-3148ee6706e9
     }
    }

After you serve your app and enter the uuidTestPage you should see the id logged to the console. The format of the id will vary depending on the version you use:

Version 1 (timestamp): my example.

Version 3 (namespace)

Version 4 (random) etc...

Happy coding!

Cai Cruz
  • 103
  • 1
  • 2
  • 12
0

you can try it: (angular2-uuid)

npm install angular2-uuid --save

......

import { UUID } from 'angular2-uuid';
...
let uuid = UUID.UUID();

it works on angular 2 & ionic 2

M.Hajavi
  • 277
  • 4
  • 17
  • Beware of using that package! It is not compliant to RFC4122. https://github.com/wulfsolter/angular2-uuid/issues/10 – Niel de Wet Oct 30 '17 at 10:37