Skip to content

Why does my CloudWatch canary fail with the "Cannot find module" error?

2 minute read
0

I want to resolve the "Cannot find module '/opt/nodejs/node_modules/myfunction" error in my Amazon CloudWatch canary.

Resolution

Your .zip deployment package must contain the required directory structure, with your function and dependencies organized under the nodejs/node_modules path. If you create a .zip file without this required directory structure, then you receive the following error:

"Cannot find module '/opt/nodejs/node_modules/myfunction."

To resolve the error, complete the following steps:

  1. Run the following command to create the nodejs/node_modules structure:

    mkdir -p /nodejs/node_modules/
  2. Copy your CloudWatch canary function to /nodejs/node_modules/:

    cp CanaryFunction.js /nodejs/node_modules/cp -R /lib /nodejs/node_modules/

    Note: Set your canary's script entry point to myCanaryFilename.functionName to match the file name of your script's entry point. For example, if your canary file is CanaryFunction.js, then set the handler to CanaryFunction.handler.

  3. To create a .zip file that contains the contents of your project folder at the top of the nodejs directory, run the following command:

    zip -r Canary.zip ./nodejs/*

When you write a canary script that has multiple .js files or script dependencies, bundle all files into a single .zip file. Use the nodejs/node_modules folder structure. For example, you can use nodejs/node_modules/myCanaryFilename.js.

AWS OFFICIALUpdated 7 months ago