How to use unbundled JavaScript Lamda Functions locally?

Hey guys!

This is my first time trying to use unbundled JavaScript Lamba Functions. The Function works perfectly when deployed, but I can’t get it to work locally for testing before releasing it to production.

Here’s what I’ve done:

image

Added a package.json with the required dependencies in the root of my project folder → installed Node Modules → imported them in Function as const foo = require('bar');.

The thing is, the function hello.js works perfectly since it’s not using Node Modules at all. I just created this one to test if I was using functions correctly and I did get a Hi! printed on my screen, locally, as well as in production.

exports.handler = async (event, context) => {
  return {
    statusCode : 200,
    body: 'Hi!'
  };
};

However, the function getSecret.js gives me ERR_TOO_MANY_REDIRECTS. If I use netlify dev --edgeHandlers command, I get Function not found. In any case, I can’t get this to work locally. I just wanted to be sure that I’m not doing anything wrong before I ask this on Netlify Dev’s GitHub issues.

Hey there, @hrishikesh Really good question. Do you have a site name you can share for the site you tried this on?

in addition to Hillary’s question, I’m wondering what your netlify.toml looks like and if you have any redirects in it? Have you been testing with invoke as shown here? cli/netlify-dev.md at master · netlify/cli · GitHub

1 Like

@hillary Yes, it’s working fine on this site: flamboyant-lumiere-eec5e6.

@jen, yes I do have redirects in my netlify.toml:

[build]
  command = "hugo"
  publish = "public"
  functions = "functions"

[build.environment]
  HUGO_ENV = "production"
  HUGO_VERSION = "0.82.0"

[[redirects]]
  from = "/*"
  to = "/404/"
  status = 404

Here’s my entire repo if it helps: GitHub - Hrishikesh-K/Blogfolio at test2

However, I had read this thread: Standard 404 rule causes infinite redirect when running static Hugo site with dev command · Issue #1595 · netlify/cli · GitHub, I tried the steps mentioned here which is why I used netlify dev --edgeHandlers command and got a different result as I have mentioned. But to be safe, I even deleted the redirect rules and restarted the server, that didn’t help too.

About the invoke thing, I’m not sure how to use it. I ran netlify functions:invoke without the dev server running and got the connection refused error in terminal. I started the dev server and typed netlify functions:invoke, the dev server crashed. Is there any specific way to use it?

Hey @hrishikesh,
Thanks for sharing that repo. I cloned it (specifically, test2 branch) and was able to invoke the function so I’ll share my steps below and we can see where we may be doing something different:

  1. Clone repo, pull test2 branch, cd into the Blogfolio directory
  2. npm install to install dependencies
  3. in a separate terminal window, run netlify dev to start dev server
  4. back in the other window, run netlify functions:invoke; the CLI prompts me to confirm which function to trigger, I confirm getSecret
  5. CLI prompts “Invoke with emulated…”, I select no

Initially, I got “Error: Cryptr: secret must be a non-0-length string” so I removed your process.env variable and replaced it with the string test

After making that change and invoking again, I saw the POST and 200 status response.

I should also note my OS/version info:

  • netlify-cli/3.4.7
  • OS darwin-x64
  • node-v15.6.0.

Want to give that a try and see how it goes?

That’s it! I had completely forgotten that I could always open another terminal window! :sweat_smile: That’s the only step I missed in my previous setup. But it’s working now! Thanks a lot @jen!

2 Likes

Yay! Glad to hear it’s up and running :slight_smile:

1 Like