node.js - How to load a lib folder in aws lambda? -


in serverless, have following folder structure

/component_a/function_1/function_1.js /component_a/lib/util.js 

when try load util.js function_1.js using

u = require('../lib/util.js') 

it works serverless cli "serverless function run function_1". in lambda/api-gateway cannot find lib/util.js .

this error "error: cannot find module '../lib/util'"

how can fix it?

this how fix. in component_a/s-function.json replace

"handler": "handler.handler", 

with

"handler": "component_a/handler.handler", 

in function_1.js call util.js like

u = require('../lib/util') 

from serverless documentation

the handler property gives ability share code between functions. default handler property handler.handler, means it's relative function folder, function folder deployed lambda.

if want include parent subfolder of function, should change handler this: functionname/handler.handler can see, path handler includes function folder, means path relative parent subfolder, in case parent subfolder deployed along function. if have lib folder in parent subfolder required function, it'll deployed function.

this gives ability handle npm dependencies like. if have package.json , node_modules in parent subfolder, it'll included in deployed lambda. more parent folders include in handler path, higher go in file tree.