By using AWS re:Post, you agree to the AWS re:Post Terms of Use

Runtime.ImportModuleError despite having specified the dependencies in package.json

0

I used '@azure/identity' package for my Lambda function. If I import but doesn't use the module, the Lambda function is error-free, however when I actually use the module by adding this code:

class SharePointFileUploader { // private client: Client; constructor( private tenantId: string, private clientId: string, private clientSecret: string ) { // // Create credential const credential = new identity.ClientSecretCredential( this.tenantId, this.clientId, this.clientSecret ); }

I encountered this issue when trying to test the function locally use SAM local invoke. {"errorType": "Runtime.ImportModuleError", "errorMessage": "Error: Cannot find module '@azure/identity'\nRequire stack:\n- /var/task/statistics.js\n- /var/runtime/index.mjs", "trace": ["Runtime.ImportModuleError: Error: Cannot find module '@azure/identity'", "Require stack:", "- /var/task/statistics.js", "- /var/runtime/index.mjs", " at _loadUserApp (file:///var/runtime/index.mjs:1087:17)", " at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1119:21)", " at async start (file:///var/runtime/index.mjs:1282:23)", " at async file:///var/runtime/index.mjs:1288:1"]}.

This is my dependencies included in the package.json file: "dependencies": { "@aws-cdk/aws-lambda": "^1.203.0", "@aws-sdk/client-s3": "^3.698.0", "@aws-sdk/client-secrets-manager": "^3.696.0", "@azure/identity": "^4.5.0", "@microsoft/microsoft-graph-client": "^3.0.7", "@types/aws-lambda": "^8.10.145", "aws-cdk-lib": "2.168.0", "aws-sdk": "^2.1692.0", "constructs": "^10.0.0", "node-fetch": "^3.3.2" },

Can someone please explain why this happened? Thank you so much in advance!

asked 4 days ago22 views
1 Answer
0

The error you're encountering is likely due to the way Lambda packages and deploys your function. When you're testing locally using SAM (Serverless Application Model), it's trying to simulate the Lambda environment, but it seems that the '@azure/identity' module is not being included in your deployment package.

Here are a few things to check and try:

  1. Make sure you've run 'npm install' in your project directory to install all the dependencies listed in your package.json file.

  2. When using SAM to test locally, it doesn't automatically include node_modules. You need to ensure that your dependencies are properly packaged. Try running 'sam build' before 'sam local invoke' to package your function correctly.

  3. Check if you're using the correct Node.js runtime version that's compatible with all your dependencies.

  4. Ensure that your Lambda function's handler is correctly specified in your SAM template (template.yaml or template.json file).

  5. If you're using ES modules (indicated by "type": "module" in package.json), make sure your import statements are correct. For CommonJS, use require() instead of import.

  6. Try adding a "prepare" script in your package.json to install dependencies: "scripts": { "prepare": "npm install" }

  7. If the issue persists, you might need to explicitly include the node_modules in your deployment package. You can do this by modifying your SAM template to include a custom packaging instruction.

Remember, when you deploy to AWS Lambda, you need to include all dependencies in the deployment package. Local testing with SAM should mimic this environment, but sometimes additional configuration is needed to ensure all modules are properly included.
Sources
Locally invoke Lambda functions with AWS SAM - AWS Serverless Application Model
Working with AWS Lambda functions using the AWS Toolkit - AWS Cloud9
Troubleshoot deployment issues in Lambda - AWS Lambda

profile picture
answered 4 days ago
  • Addtional note: I use AWS CDK to deploy the Lambda function. I have received some answers that I should package node_modules in the Lambda function with the actual code or put it in the layer wrapping the Lambda function. However, I still don't understand why I have to do that because with other packages, simply define them in the package.json is sufficient?

    Sorry if the answer is obvious, I'm new to AWS so can someone please kindly explain?

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions