如何在 aws lambda 中加载 lib 文件夹?
How to load a lib folder in aws lambda?
在 Serverless 中,我有以下文件夹结构
/component_a/function_1/function_1.js
/component_a/lib/util.js
当我尝试使用
从 function_1.js 加载 util.js 时
u = require('../lib/util.js')
它在无服务器 CLI 中运行 "serverless function run function_1"。但是在 lambda/api-gateway 它找不到 lib/util.js .
这是错误"Error: Cannot find module '../lib/util'"
我该如何解决?
这是修复方法。在component_a/s-function.json中替换
"handler": "handler.handler",
和
"handler": "component_a/handler.handler",
在function_1.js中调用util.js喜欢
u = require('../lib/util')
来自无服务器文档
The handler property gives you the ability to share code between your
functions. By default the handler property is handler.handler, that
means it's only relative to the function folder, so only the function
folder will be deployed to Lambda.
If however you want to include the parent subfolder of a function, you
should change the handler to be like this:
functionName/handler.handler
As you can see, the path to the handler
now includes the function folder, which means that the path is now
relative to the parent subfolder, so in that case the parent subfolder
will be deployed along with your function. So if you have a lib folder
in that parent subfolder that is required by your function, it'll be
deployed with your function.
This also gives you the ability to handle npm dependencies however you
like. If you have a package.json and node_modules in that parent
subfolder, it'll be included in the deployed lambda. So the more
parent folders you include in the handler path, the higher you go in
the file tree.
在 Serverless 中,我有以下文件夹结构
/component_a/function_1/function_1.js
/component_a/lib/util.js
当我尝试使用
从 function_1.js 加载 util.js 时u = require('../lib/util.js')
它在无服务器 CLI 中运行 "serverless function run function_1"。但是在 lambda/api-gateway 它找不到 lib/util.js .
这是错误"Error: Cannot find module '../lib/util'"
我该如何解决?
这是修复方法。在component_a/s-function.json中替换
"handler": "handler.handler",
和
"handler": "component_a/handler.handler",
在function_1.js中调用util.js喜欢
u = require('../lib/util')
来自无服务器文档
The handler property gives you the ability to share code between your functions. By default the handler property is handler.handler, that means it's only relative to the function folder, so only the function folder will be deployed to Lambda.
If however you want to include the parent subfolder of a function, you should change the handler to be like this: functionName/handler.handler As you can see, the path to the handler now includes the function folder, which means that the path is now relative to the parent subfolder, so in that case the parent subfolder will be deployed along with your function. So if you have a lib folder in that parent subfolder that is required by your function, it'll be deployed with your function.
This also gives you the ability to handle npm dependencies however you like. If you have a package.json and node_modules in that parent subfolder, it'll be included in the deployed lambda. So the more parent folders you include in the handler path, the higher you go in the file tree.