Using netlify dev with unbundled functions and GitHub-based CD

I can’t find the (explicit) answer to what I would have thought would be a very common question: can I use netlify dev with unbundled functions and also use GitHub-based CD without having to have a netlify-lambda build step (or indeed any build step for the functions)?

The Netlify docs and the netlify-lambda readme both say that you can use netlify dev and unbundled function scripts as of CLI v2.7. That’s great, but I’m not using the CLI - I’m using Git-based CD. (Actually I was using the CLI to deploy and indeed this did work perfectly, but I moved to Git-based CD and now the functions fail to build due to not having the deps installed.)

FWIW I added the functions based on netlify functions:create with one of the FaunaDB starters (which I then customised to work how I want), and it gave me a new dir for the function with a function-name.js file, package.json and a package-lock.json and a node_modules dir (function best practice, yay). node_modules is obviously not committed to Git so I think this is why the build failed. This suggests to me that maaaybe ZISI is trying to do its work on deploy to do the bundling for me (yay), but not finding the packages to zip - i.e. it only zips and ships, but doesn’t first install deps with npm install for some reason. But I can’t say that with any real certainty, just a hunch.

However, I’m surprised that code that is generated by netlify functions:create, supported just fine by netlify dev and netlify deploy just fails when used with the CD. As such, I figured I’d check whether there’s something I’m doing wrong.

I have previously (before netlify dev was out, or at least properly out) used netlify-lambda to do local dev as well as including it in the build command for CD deployments. That works, but I’d be surprised if I still have to do this now I’m not in actual need of netlify-lambda locally.

Am I missing something?

Also, if I do now need to use netlify-lambda to bundle my unbundled (but otherwise not in need of transpilation) functions, do I need to change it to use the single functions dir with file-per-function entrypoint? What about the best practice of multiple directories each with their own package.json? I’m confused. It seems like the “new” netlify dev unbundled functions stuff and the “old” netlify-lambda ways are kinda working against each other. But it seems like netlify dev unbundled functions is made to work with CLI deploys only, and for CD deploys you need to have a build script to make these massive single JS files (be that netlify-lambda or your own custom way of doing the same I guess.

Any pointers?

Thanks.

Okay turns out I was right - ZISI will do bundling for me, so no netlify-lambda required (phew!) but the package.json deps are not installed for some reason.

So I can either move all dep requirements to the root package.json (yuck!) or include cd lambda/function1 && npm install for each function as part of my build script (also yuck but for now best option).

Hopefully the Netlify team can correct me here and offer a much nicer solution/tell me that it should work and I’m just doing something wrong…

The three ways I know of to deploy a function are shown in code, here:

I am not an expert, but based on those, I believe that your best options are either:

  1. unbundled BUT packaged up with node_modules via zip before committed, so…that’s also yuck in my book :stuck_out_tongue: .
  2. using your cd && npm install trick, which eats a bit more build time on our side so you don’t have to commit huge binaries to GH.

Cheers @fool, Yeah I’ve gone with option 2 at the moment, but might I suggest as a “feature request” that when Netlify is in CD mode, it looks for package.json files inside the “functions dir” (that aren’t also in a node_modules directory/that don’t have a corresponding node_modules directory so we can avoid the use-case of uploading all deps, which this is supposed to supersede) and if it finds them, runs through npm install automatically like it does for the main one before running ZISI?

Presumably the reason it with with the CLI (using netlify deploy) is because locally I have already installed the deps, and the CLI is just running a form of ZISI, or uploads the entirety of each function’s node_modules so the remote build system can ZISI with a full directory tree. So yeah, I think this just needs to be unified.

Hi Alex, I think a build plugin could address your feature request when it comes out. Here’s a doc on how a plugin is written. What do you think?

Yup definitely looks like that could solve it nicely. Still feels to me that it should be part of the core Netlify offering, because it’s already “solved” in various other Netlify workflows (e.g. using a netlify-lambda build step, CLI-based deployments).

But if Netlify chooses not to support this directly (maybe this is the exact point of build plugins - to thin down the core offering), then yeah, doing it as a build plugin would totally be doable.

1 Like