Using ES Modules breaks when a directory with the same name as the module exists
還沒有人認領這個 Issue。
評估
- 難度
- 2/5
- 預估耗時
- 1-3 小時
- 新手友好度
- 35/100
- Issue 類型
- 缺陷
- 描述清晰度
- 描述清楚
- 活躍度
- 停滯
- 技術堆疊
- node.js, typescript
- 領域
- backend
研究方向
閱讀 src/UserFunction.js 中第 73 行及第 133-174 行附近的內容,然後使用 Node.js 20 runtime 的 verbose 模式,透過所述的 package.json、hello-world.js 和 hello-world/ 設定重現此問題。當目錄與模組同名時,handler 能夠成功載入,且不會出現回報的 ImportModuleError,即表示完成。
由索引模型根據 Issue 內容生成。
描述
If you setup a project with the following structure:
"type": "module"in package.json- A
hello-world.jsfile - A
hello-world/folder - Use
hello-world.lambdaHandleras your module/handler - Enable verbose mode, e.g. with
ENV AWS_LAMBDA_RUNTIME_VERBOSE 1in a Docker container usingpublic.ecr.aws/lambda/nodejs:20, so you can see the verbose messages
This is the resulting log.
START RequestId: 99e7b316-ab71-4bd2-8aed-85df8b0aeaaf Version: $LATEST
2023-12-13T01:13:42.274Z undefined INFO RUNTIME LOADER Try loading as commonjs: hello-world with paths: , /var/task
2023-12-13T01:13:42.275Z undefined INFO RUNTIME LOADER Try loading as commonjs: /var/task/hello-world
2023-12-13T01:13:42.276Z undefined INFO RUNTIME LOADER globalPaths ["/opt/nodejs/node20/node_modules","/opt/nodejs/node_modules","/var/runtime/node_modules","/var/runtime","/var/task","/root/.node_modules","/root/.node_libraries","/var/lang/lib/node"]
2023-12-13T01:13:42.276Z undefined ERROR Uncaught Exception {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module '/var/task/hello-world'\nRequire stack:\n- /var/runtime/index.mjs","stack":["Runtime.ImportModuleError: Error: Cannot find module '/var/task/hello-world'","Require stack:","- /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"]}
13 Dec 2023 01:13:42,288 [ERROR] (rapid) Init failed error=Runtime exited with error: exit status 129 InvokeID=
2023-12-13T01:13:42.351Z undefined INFO RUNTIME LOADER Try loading as commonjs: hello-world with paths: , /var/task
2023-12-13T01:13:42.352Z undefined INFO RUNTIME LOADER Try loading as commonjs: /var/task/hello-world
2023-12-13T01:13:42.353Z undefined INFO RUNTIME LOADER globalPaths ["/opt/nodejs/node20/node_modules","/opt/nodejs/node_modules","/var/runtime/node_modules","/var/runtime","/var/task","/root/.node_modules","/root/.node_libraries","/var/lang/lib/node"]
2023-12-13T01:13:42.353Z undefined ERROR Uncaught Exception {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module '/var/task/hello-world'\nRequire stack:\n- /var/runtime/index.mjs","stack":["Runtime.ImportModuleError: Error: Cannot find module '/var/task/hello-world'","Require stack:","- /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"]}
END RequestId: a879dd58-2f3f-458a-9911-dbb92b552019
REPORT RequestId: a879dd58-2f3f-458a-9911-dbb92b552019 Init Duration: 0.29 ms Duration: 156.42 ms Billed Duration: 157 ms Memory Size: 128 MB Max Memory Used: 128 MB
{"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module '/var/task/hello-world'\nRequire stack:\n- /var/runtime/index.mjs","trace":["Runtime.ImportModuleError: Error: Cannot find module '/var/task/hello-world'","Require stack:","- /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"]}
Cause
Before trying to load a module an extensionless import is attempted. Because fs.existsSync does not differentiate between a directory and a file it returns true and a require on the directory is attempted without any try catch.
Background
I was working on a project using multiple ESModules based lambda functions. The code was transpiled from TypeScript and because some code needed to be shared between functions I had a hello-world.js in the root that was export * from './dist/hello-world/src/app.js'; to so the lambda function ran off the transpiled code.
- 主要語言
- TypeScript
- 星號
- 224
- 分支
- 60
- 平均合併
- 26 分鐘
- 30 天內合併 PR
- 2
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
aws/aws-lambda-nodejs-runtime-interface-client 的其他 Issue
-
難度 5/5 一週以上 新手友好度 20/100
aws/aws-lambda-nodejs-runtime-interface-client#198 · 3 則留言 · 6 個 reaction ·
-
難度 4/5 3-5 天 新手友好度 48/100
aws/aws-lambda-nodejs-runtime-interface-client#195 · 12 個 reaction ·
-
bug
aws/aws-lambda-nodejs-runtime-interface-client#170 · 15 則留言 · 6 個 reaction · 已指派 1 人 ·
-
難度 2/5 1-3 小時 新手友好度 38/100
aws/aws-lambda-nodejs-runtime-interface-client#159 · 2 個 reaction ·
-
難度 4/5 3-5 天 新手友好度 42/100
aws/aws-lambda-nodejs-runtime-interface-client#145 · 1 則留言 · 6 個 reaction ·
查看 aws/aws-lambda-nodejs-runtime-interface-client 的全部 Issue
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 74/100
ontola/atomic-server#1625 ·
-
bug
難度 2/5 1-3 小時 新手友好度 70/100
melgarafael/DeskcommCRM#1451 ·
-
難度 1/5 1 小時以內 新手友好度 82/100
-
bug via-triage
難度 2/5 1-3 小時 新手友好度 78/100
-
bot:ai-assisted component:compact-js status:untriaged
難度 2/5 1-3 小時 新手友好度 84/100
midnightntwrk/midnight-sdk#403 ·