SyntaxError: Cannot use import statement outside a module #26864
Unanswered
dev-samples
asked this question in
Q&A
Replies: 2 comments
-
The error message indicates a syntax error in your code related to the use of the export keyword. Please replace it with 'exports' and let me know if that works for you! |
Beta Was this translation helpful? Give feedback.
0 replies
-
I was doing the C# demo and ran into the same issue with the js lambda. I'm not a JavaScript guy, especially the older versions and that lambda JavaScript is older. What did to get the demo to work was:
Files: |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
UPDATE:
Below are changes needed to make the sample projet working from:
https://docs.aws.amazon.com/cdk/v2/guide/serverless_example.html
1: import -> require and module.exports = {handler}
Based on:
https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/sdk-example-javascript-syntax.html
Convert to
require
and do2: Fix lambda handler name in MyWidgetService/lib/widget_service.ts
When getting this error:
2023-08-24T08:47:06.485Z undefined ERROR Uncaught Exception {"errorType":"Runtime.HandlerNotFound","errorMessage":"widgets.main is undefined or not exported","stack":["Runtime.HandlerNotFound: widgets.main is undefined or not exported"," at UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1096:15)"," at async start (file:///var/runtime/index.mjs:1256:23)"," at async file:///var/runtime/index.mjs:1262:1"]}
Change:
MyWidgetService/lib/widget_service.ts
To:
3: < HTTP/2 500 Cannot read properties of undefined (reading 'length')
when curling:
From web console something else:
which is as expected.
But would make sense if the sample projected worked out of the box.
I am trying out this example for deploying a serverless application (api gateway + lambda):
https://docs.aws.amazon.com/cdk/v2/guide/serverless_example.html
that I have deployed (cdk deploy) without introducing any changes to the sample files provided in above guide.
But when I test the lambda via the web console I get:
From below tsconfig.json file it seems this generated sample project is a commonjs project which use require instead of import:
https://www.scaler.com/topics/nodejs/require-vs-import-nodejs/
https://stackoverflow.com/questions/76065961/syntaxerror-unexpected-token-export-in-aws-lambda-node-14-x
https://stackoverflow.com/questions/70849225/how-to-solve-cannot-use-import-statement-outside-a-module-in-aws-lambda-console
So if I change the handler resources/widgets.js (again copy/pasted directly from the guide above) from:
To:
Deploy that and run the test again from the web console I now get:
Any input?
and the generated package.json for the above hello world project:
tsconfig.json
Beta Was this translation helpful? Give feedback.
All reactions