Search code examples
node.jsamazon-web-servicesaws-lambdauuidserverless-framework

When using uuid in aws lambda results undefined in logs


I am using uuid in my lambda as:

import uuid from 'uuid';

Item: {
  userId: uuid.v4(),
  name: '',
  email: '',
}

When the lambda get triggered it results following error.

ERROR Invoke Error { "errorType": "TypeError", "errorMessage": "Cannot read property 'v4' of undefined", .... }

How do I fix this.

UUID version: 7.0.1


Solution

  • After spending hours found that the latest version of uuid has a breaking change. Hope this would help anyone who is facing the same problem.

    If you are importing and using uuid as

    import uuid from 'uuid';
    
    id: uuid.v4()
    

    This won't work anymore

    as per the document here

    You will need to import it as

    import { v4 } from 'uuid';
    

    or use a custom namespace

    import { v4 as uuidv4 } from 'uuid';
    

    This will solve the problem.