Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
595972434 opened this issue Apr 6, 2023 · 3 comments
Closed

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

595972434 opened this issue Apr 6, 2023 · 3 comments
Labels
area/build sam build command area/dependencies Updates a dependency

Comments

@595972434
Copy link

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:

  • sam init, AWS Quick Start Templates, Hello World Example, nodejs18.x Zip, Hello World Example TypeScript
  • cd hello-world , npm install jsonpath, npm i --save-dev @types/jsonpath
  • Update app.ts code
  • sam build
  • sam local start-api
  • curl http://127.0.0.1:3000/hello

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

@595972434 595972434 added the stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. label Apr 6, 2023
@qingchm qingchm added area/build sam build command area/dependencies Updates a dependency and removed stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. labels Apr 6, 2023
@jfuss
Copy link
Contributor

jfuss commented Apr 11, 2023

@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
Copy link
Author

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
Copy link
Contributor

⚠️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.

@595972434 595972434 changed the title Bug: SAM Template for Typescript can't import npm package Bug: SAM Template for Typescript can't import json-path package Apr 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/build sam build command area/dependencies Updates a dependency
Projects
None yet
Development

No branches or pull requests

3 participants