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.

aws-lambda-nodejs: Default entry finding fails when defining file starts with "file://"

See original GitHub issue

Describe the bug

Attempting to create a lambda with NodejsFunction, which should auto find the handler based on the defining files path.

I get an error that looks like this:

Error: Cannot find handler file file:///Users/stack.handler.ts, file:///Users/<path>/stack.handler.js or file:///Users/<path>/stack.handler.mjs

where the middle of the path has been replaced with <path> for brevity and privacy.

This implies the paths being checked start with “file://”, which I’ve tested and does not work with Node 16.13.1.

Those paths are produced from this function, which I believe is incorrect.

This is running on macos 12.5, with the a fore mentioned version of nodejs.

Expected Behavior

I expected it to find the file, as the path was correct without the file:// prefix.

Current Behavior

Error: Cannot find handler file file:///Users/stack.handler.ts, file:///Users/<path>/stack.handler.js or file:///Users/<path>/stack.handler.mjs

Reproduction Steps

Create a lambda function using NodejsFunction with no entry parameter specified, and synthesize.

Possible Solution

callsites() produces paths that start with file:// when they are local files, and findDefiningFile doesn’t scrub the path. Maybe make findDefining file remove the prefix when it’s file://

Additional Information/Context

No response

CDK CLI Version

2.37.1 (build f15dee0)

Framework Version

No response

Node.js Version

16.13.1

OS

macos 12.5

Language

Typescript

Language Version

4.7.2

Other information

No response

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:3
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jonbarksdalecommented, Sep 8, 2022

Thanks @okko, appreciate you stepping in with the reproduction.

0reactions
okkocommented, Nov 2, 2022

Workaround until issue is resolved in aws-cdk:

const handler = new lambdaNodejs.NodejsFunction(scope, functionId, {
  entry: new URL(import.meta.url.replace(/(.*)(\..+)/, '$1.' + functionId + '$2')).pathname, // https://github.com/aws/aws-cdk/pull/21802#issuecomment-1249940400
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Lambda can't find modules from outer folders when deployed ...
I tried copying node_modules folder and other files (which were outside the folder get-users ) to the folder get-users and it worked perfectly....
Read more >
AWS Lambda Nodejs - AWS Documentation - Amazon.com
No information is available for this page.
Read more >
@aws-cdk/aws-lambda-nodejs - npm
By default, the construct will use the name of the defining file and the construct's id to look up the entry file:
Read more >
How to use source maps in AWS Lambda with Node.js
To display the actual file path and the line of the error, ... so let's start by creating the “functions” folder in the...
Read more >
@aws-cdk/aws-lambda-nodejs | Yarn - Package Manager
Node.js Function. The NodejsFunction construct creates a Lambda function with automatic transpiling and bundling of TypeScript or Javascript code. This results ...
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