Mikerizzo
Mikerizzo

Reputation: 601

Serverless - Lambda Layers "Cannot find module 'request'"

When I deploy my serverless api using:

serverless deploy

The lambda layer gets created but when I go to run the function is gives me this error:

"Cannot find module 'request'"

But if I upload the .zip file manually through the console (the exactly same file thats uploaded when I deploy), it works fine.

Any one have any idea why this is happening?

environment:
SLS_DEBUG: "*"

provider:
name: aws
runtime: nodejs8.10
stage: ${opt:api-type, 'uat'}-${opt:api, 'payment'}
region: ca-central-1
timeout: 30
memorySize: 128
role: ${file(config/prod.env.json):ROLE}
vpc:
    securityGroupIds:
    - ${file(config/prod.env.json):SECURITY_GROUP}
    subnetIds:
    - ${file(config/prod.env.json):SUBNET}
apiGateway:
    apiKeySourceType: HEADER
apiKeys:
    - ${file(config/${opt:api-type, 'uat'}.env.json):${opt:api, "payment"}-APIKEY}

functions:
- '${file(src/handlers/${opt:api, "payment"}.serverless.yml)}'

package:
# individually: true
exclude:
    - node_modules/**
    - nodejs/**

plugins:
- serverless-offline
- serverless-plugin-warmup
- serverless-content-encoding

custom:
contentEncoding:
    minimumCompressionSize: 0 # Minimum body size required for compression in bytes

layers:
nodejs:
    package:
    artifact: nodejs.zip
    compatibleRuntimes:
    - nodejs8.10
    allowedAccounts:
    - "*"

Thats what my serverless yaml script looks like.

Upvotes: 10

Views: 15088

Answers (4)

pgeimer
pgeimer

Reputation: 103

I am using typescript with the serverless-plugin-typescript and I was having a same error, too.

When I switched from

const myModule = require('./src/myModule');

to

import myModule from './src/myModule';

the error disappeared. It seems like the files were not included into the zip file by serverless when I was using require.

PS: Removing the serverless-plugin-typescript and switching back to javascript also solved the problem.

Upvotes: 0

MrAlex
MrAlex

Reputation: 21

If anyone face a similar issue Runtime.ImportModuleError, is fair to say that another cause of this issue could be a package exclude statement in the serverless.yml file.

Be aware that if you have this statement:

package:
  exclude:
    - './**'
    - '!node_modules/**'
    - '!dist/**'
    - '.git/**'

It will cause exactly the same error, on runtime once you've deployed your lambda function (with serverless framework). Just, ensure to remove the ones that could create a conflict across your dependencies

Upvotes: 2

simsketch
simsketch

Reputation: 3042

Make sure you run npm install inside your layers before deploying, ie:

cd ~/repos/repo-name/layers/utilityLayer/nodejs && npm install

Otherwise your layers will get deployed without a node_modules folder. You can download the .zip of your layer from the Lambda UI to confirm the contents of that layer.

Upvotes: 2

codeinaire
codeinaire

Reputation: 1864

I was having a similar error to you while using the explicit layers keys that you are using to define a lambda layer.

My error (for the sake of web searches) was this:

Runtime.ImportModuleError: Error: Cannot find module <package name>

I feel this is a temporary solution b/c I wanted to explicitly define my layers like you were doing, but it wasn't working so it seemed like a bug.

I created a bug report in Serverless for this issue. If anyone else is having this same issue they can track it there.

SOLUTION

I followed this this post in the Serverless forums based on these docs from AWS.

I zipped up my node_modules under the folder nodejs so it looks like this when it is unzipped nodejs/node_modules/<various packages>.

Then instead of using the explicit definition of layers I used the package and artifact keys like so:

layers:
  test:
    package:
      artifact: test.zip

In the function layer it is referred to like this:

functions:
  function1:
    handler: index.handler
    layers:
      - { Ref: TestLambdaLayer }

The TestLambdaLayer is a convention of <your name of layer>LambdaLayer as documented here

Upvotes: 5

Related Questions