question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ImportModuleError when executing lambdas

See original GitHub issue

When I add the es-build plugin to my serverless.yml file and deploy to AWS, I get the following issue when triggering a lambda called InternalConfig:

{
    "errorType": "Runtime.ImportModuleError",
    "errorMessage": "Error: Cannot find module 's_internalConfig'\nRequire stack:\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
    "stack": [
        "Runtime.ImportModuleError: Error: Cannot find module 's_internalConfig'",
        "Require stack:",
        "- /var/runtime/UserFunction.js",
        "- /var/runtime/index.js",
        "    at _loadUserApp (/var/runtime/UserFunction.js:100:13)",
        "    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
        "    at Object.<anonymous> (/var/runtime/index.js:43:30)",
        "    at Module._compile (internal/modules/cjs/loader.js:1072:14)",
        "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)",
        "    at Module.load (internal/modules/cjs/loader.js:937:32)",
        "    at Function.Module._load (internal/modules/cjs/loader.js:778:12)",
        "    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)",
        "    at internal/main/run_main_module.js:17:47"
    ]
}

I get the same issue for all my lambdas deployed through serverless when using serverless-esbuild

For context, the InternalConfig lambda is defined in the serverless.yml file as follows:

  internalConfig:
    handler: './src/handlers/internal-config.main'
    events:
      - http:
          method: get
          path: /internal/config/{serviceName}
      - http:
          method: post
          path: /internal/config/{serviceName}

What’s weird is this error does not exist if I swap out the serverless-esbuild plugin for another zero-config typescript support plugin like serverless-plugin-typescript.

Any ideas as to why this might be happening?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:7

github_iconTop GitHub Comments

1reaction
ghostcommented, Nov 15, 2021

You are right, declaring winston logger as an external package in my case worked correctly. Thanks! 🙇

1reaction
ghostcommented, Nov 12, 2021

Hi i ran into the same problem. Esbuild does not bundle objects outside the handler (if for example, You want to initialise something and cache it between invocations)

// handler.js
import { logger } from "logger"; // some external package

const logger = Logger();

export const handler = async (event, context) => {

  logger.info("Event: ", event); // errror cannot find module logger in runtime
  return "Hello";
};

A workaround is to initialise your code in a handler, which is sometimes not a good option cc @samchungy

Read more comments on GitHub >

github_iconTop Results From Across the Web

Resolve the "Unable to import module" error for Lambda code ...
I receive the "Unable to import module" error when I try to run my AWS Lambda code in Node.js. Short description. You typically...
Read more >
"Runtime.ImportModuleError" trying to access npm package in ...
I was zipping a file containing my lambda instead of just my lambdas root. This was causing the lambda to look for my...
Read more >
Lambda fails: "Runtime.ImportModuleError: Error: Cannot find ...
RUN rm -rf node_modules RUN npm install CMD serverless deploy. The lambda's stack trace is as follows: { "errorType": "Runtime.
Read more >
How to Solve a AWS Lambda error - Runtime.ImportModuleError
ImportModuleError: Error: Cannot find module 'jmespath'. AWS Lambda scripts written in nodejs8 were deprecated. You run the same scripts via ...
Read more >
AWS Lambda – cannot find module (based on handler)
Error in the Execution Result. <pre> Response: { "errorType": "Runtime.ImportModuleError", "errorMessage": "Error: Cannot find module 'decode-verify-jwt'", ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found