-
Notifications
You must be signed in to change notification settings - Fork 115
Does Netlify Lambda support sub-directory ? #90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
confirmed, your functions need to be top level js (or ts or mjs) files. this is consistent with real deployed netlify functions behavior. pr for docs or warnings welcome! |
Currently, I use a hack , i.e., set a redirect rule in [[redirects]]
from = "/hello/world"
to = "/.netlify/functions/hello-world"
status = 200
force = true But it's a hack after all. I prefer that the source code directory structure would be Does netlify lambda support such feature ? How do I make it work ? That's my question. And I have searched a lot and got no example, no doc or any other references. And I would love to submit a pr to the docs, which would include a demo. |
Hey, I made it! I set up a file named let lify = require('./net/lify');
exports.handler = function (event, context, callback) {
let subPath = event.path.split('/')[2];
switch (subPath) {
case 'lify':
lify(event, context, callback)
break;
default:
callback('404 Path Not Found!');
}
} I don't need a redirect rule in the It's still not that kind elegant solution I expect though. It seems the Netlify Lambda uses the express framework to find the path and module. I am not familiar with it. So,.... |
yeah. i'm glad you figured it out. remember that in the end we're still only locally emulating whatever is being done in the real Netlify Functions. so if they dont support subdirectories than we dont either. |
Right. Thanks for reply. |
By the way, how do I add something to the docs ? I cannot find the doc repo and there is no button in the doc page to let me "Edit" or something. |
the Netlify docs are unfortunately not open source. you can just ping netlify.com/support with your suggestions. the netlify-lambda docs however... just a README on this repo |
@connor11528 you can place the lambda folder where-ever you want. this issue is for putting yet another folder inside that folder and expecting that to be reflected in the URL for the function. netlify functions dont work like that unfortunately. |
@medmin Good idea, I also like to structure my lambdas in subdirectories. Here's how I've added it so I can put my lambdas in a subdir - so I can use it with File export function handler(event, context, callback) {
// Get the command from path (skip first as it is an empty string)
const [, dirName, commandStr] = event.path.split("/");
// Load the script
try {
const command = require(`./${dirName}/${commandStr}`);
return command.handler(event, context, callback);
} catch (e) {
return callback(null, {
headers: {
"content-type": "application/json"
},
statusCode: 404,
body: JSON.stringify({ msg: "Command not found" })
});
}
} And inside export function handler(event, context, callback) {
return callback(null, { ...});
} A more real world example would be Not hacky but there is probably a better/easier solution to this. |
I have deployed my Gatsby site with functions and lambda output at the root of the directory, and my project fails to build with this error: No netlify.toml found. This is needed to configure the function settings. Full log: https://paste.lucko.me/0mKlBEUqgy My netlify.toml:
My build command in package.json in the src/ dir I've intended for it to build from the parent directory: I don't get how on the one hand it says it's building the functions, and then in the other says it can't find netlify.toml file... |
Follow up, I seemed to have solved my issue with two netlify.toml files ¯_(ツ)_/¯. Seems ridiculous, but I need one at the root of my project, and it's a Gatsby monorepo. So my frontend is in /web/src, and the root netlify.toml tells where my base path to the frontend is. Then in my web directory I have another netlify.toml that contains only my functions command, by which I am building my lambda functions at the root of my project. So obviously I need one file to tell netlify where the base is, and another one in my web directory to tell it to publish the functions in the parent directory...lol |
yeah this is the standard advice for monorepos. maybe not ideal but also not super hard to do. we could always document it better. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Putting a .js file to the subfolder of functions doesn't work for me and it is mentioned many times as solution how to read local html or pdf template. What to do to achieve it?
|
Uh oh!
There was an error while loading. Please reload this page.
Hi,
I tried to use a sub-directory, such as
./netlify/functions/hello/world.js
, and I expect the url would belocalhost:9000/hello/world
.But I failed and I found nothing in the docs related to this issue.
So, I'd like to confirm that whether or not the Netlify lambda function supports such feature ? Thanks.
The text was updated successfully, but these errors were encountered: