AWS Lambda - Error: Cannot find module 'uuid/v4'

Viewed 11879

I am receiving this Error when trying to use UUID Module in AWS Lambda JavaScript code.

Error: Cannot find module 'uuid/v4'

I am not using AWSCLI or NPM, i am instead using the AWS Lambda dashboard to author the Lambda Functions in JavaScript.

const AWS = require('aws-sdk');
const AWSUUID = require('uuid/v4');
const AWSGamelift = new AWS.GameLift();

exports.handler = async (event) => 
{
 //...

Ive been searching Google nonstop trying to find a way to get the UUID Module to work. I do not want to have to setup and use an NPM environment.

Is there some method on the AWS Lambda dashboard to provide access to the UUID Module for Lambda code?

6 Answers

You need to install uuid package. uuid recently did a breaking change, the way you use it. New way is

const {"v4": uuidv4} = require('uuid');

Hope this helps.

AWS Lambda Layers is an option for this if we don't want to setup any npm environment.

Create a new Layer for your from Lambda function from the AWS console and upload the zip file of the node_modules directory containing uuid npm package.

Few things to remember to make this work:

  1. The directory structure has to be maintained as per AWS documentation https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-path

enter image description here

This is important otherwise Lambda function wont recognise the packages.

  1. Your Lambda function's IAM role has to have lambda:GetLayerVersion action permission else missing package issue will arise. This is again clearly explained in the documentation mentioned above.

Once these are done, you can execute your code. Sample response

enter image description here

nodejs folder structure is as follows

enter image description here

navigate to that file, and look around line number 3 in node_modules/expo-constants/exponentConstants.web.js

import uuidv4 from 'uuid/v4

and change it to:

import {v4 as uuidv4} from 'uuid';

This error happens because of the file structure in node_modules/uuid, if you look there is no longer a uuidv4 to import and instead they export a v4. You could change all the places where the developers wrote uuidv4 to v4 but using the { this as that } syntax you don't have to rewrite a bunch of code.

I use the full path and it works because my file is out of node_module folder:

const uuidv4 = require('./chat-server/node_modules/uuid').v4;

In mycase I need to just import uuid as it is getting used by some graphql packages.

I have a solution to all Lamba Unhandled and Cannot find module 'uuid/v4' errors. Including the one from the question asker.

First, lambda errors arise with unavailability of modules in amplify/backend/function/function_name/src/package.json file.

Taking a look at my code below, I have excluded the uuid module in this package.json file, hence the error:

Cannot find module 'uuid/v4'

{
  "name": "createorder",
  "version": "2.0.0",
  "description": "Lambda function generated by Amplify",
  "main": "index.js",
  "license": "Apache-2.0",

  "devDependencies": {
    "@types/aws-lambda": "^8.10.92"
  }
}

I got rid of the error after including the module in the package.json file.

After that, I ran the command npm install on src folder in: amplify/backend/function/function_name/src

This is the new code I ended up with:

{
  "name": "createorder",
  "version": "2.0.0",
  "description": "Lambda function generated by Amplify",
  "main": "index.js",
  "license": "Apache-2.0",
  "dependencies": {
    "uuid": "^8.2.0"
  },
  "devDependencies": {
    "@types/aws-lambda": "^8.10.92"
  }
}

Thanks! :)

Related