aws / aws-sam-cli

CLI tool to build, test, debug, and deploy Serverless applications using AWS SAM
https://aws.amazon.com/serverless/sam/
Apache License 2.0
6.51k stars 1.17k forks source link

Bug: SAM Template for Typescript can't import json-path package #4976

Closed 595972434 closed 1 year ago

595972434 commented 1 year ago

Description:

The hello-world typescript project generated by SAM init could work, but when I import a npm package jsonpath and run it locally, it raises a Runtime.ImportModuleError.

Steps to reproduce:

This is my code in app.ts

import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
import jp from "jsonpath";

export const lambdaHandler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
    const cities = [
        { name: "London", "population": 8615246 },
        { name: "Berlin", "population": 3517424 },
        { name: "Madrid", "population": 3165235 },
        { name: "Rome",   "population": 2870528 }
    ];
    const names = jp.query(cities, '$..name');

    try {
        return {
            statusCode: 200,
            body: JSON.stringify({
                message: names,
            }),
        };
    } catch (err) {
        console.log(err);
        return {
            statusCode: 500,
            body: JSON.stringify({
                message: 'some error happened',
            }),
        };
    }
};

Observed result:

{"message":"Internal server error"} and got an error from sam

{"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module '../include/module.js'\nRequire stack:\n- /var/task/app.js\n- /var/runtime/index.mjs","stack":["Runtime.ImportModuleError: Error: Cannot find module '../include/module.js'","Require stack:","- /var/task/app.js","- /var/runtime/index.mjs","    at _loadUserApp (file:///var/runtime/index.mjs:997:17)","    at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1032:21)","    at async start (file:///var/runtime/index.mjs:1195:23)","    at async file:///var/runtime/index.mjs:1201:1"]}

Expected result:

Get ["London", "Berlin", "Madrid", "Rome"] from curl command

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

{
  "version": "1.76.0",
  "system": {
    "python": "3.8.13",
    "os": "macOS-12.5.1-arm64-arm-64bit"
  },
  "additional_dependencies": {
    "docker_engine": "20.10.21",
    "aws_cdk": "2.67.0 (build b6f7f39)",
    "terraform": "Not available"
  }
}

I did some investigations on this issue. Hello-world for javascript could work, the HelloWorldFunction folder makes sense

image

But the HelloWorldFunction folder of Typescript looks a bit wired.

image

There is no node_modules under HelloWorldFunction folder. Refer to this log Mounting /path-to/sam-app3/.aws-sam/build/HelloWorldFunction as /var/task:ro,delegated, inside runtime container from sam local start-api. I think sam didn't bundle the dependences. But I do see this from sam local 🤔

Running NodejsNpmEsbuildBuilder:CopySource
Running NodejsNpmEsbuildBuilder:NpmInstall
Running NodejsNpmEsbuildBuilder:EsbuildBundle
Running NodejsNpmEsbuildBuilder:CleanUp
Running NodejsNpmEsbuildBuilder:MoveDependencies
jfuss commented 1 year ago

@595972434 npm install jsonpath doesn't update package.json right? You need to provide --save-prod for that?

Is jsonpath in the package.json?

595972434 commented 1 year ago

Hi @jfuss Thanks for your reply, I have installed the jsonpath successfully. I think that's a issue from json-path package. I use another package with SAM, it works well.

github-actions[bot] commented 1 year ago

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.