How to Test Netlify Functions Locally
I have multiple websites hosted on Netlify, and one of my favorite features is Netlify Functions. This feature allows me to add a JavaScript file with an exported function to handle requests to a specific URL. I use these functions for various purposes, such as visualizing internal data or performing operations that connect different tools I use for my business.
If you want to learn more about Netlify Functions, you can check out my Netlify Functions tutorial.
In this post, I want to discuss how to test Netlify functions locally. Recently, I needed to make changes to a live Netlify Function that handled signups for my Bootcamp. However, I didn’t want to disrupt my operations and potentially cause problems for customers. Therefore, I needed a way to test the changes locally before deploying them.
Here’s what I did:
Install the Netlify CLI by running the following command:
1
npm install -g netlify-cli
Navigate to the website folder and run the following command:
1
netlify functions:serve
This will serve the serverless functions locally on port
9999
. Now, you can access the functions using a URL like this:1
http://localhost:9999/.netlify/functions/<name>
To test the functions, you can fake a POST request using a tool like Insomnia. This allows you to simulate the functionality and ensure everything is running as expected before deploying the changes to production.
By testing the Netlify functions locally, I was able to make changes without disrupting the live environment. Additionally, this testing method considers any Netlify environment variables that you have set in the Netlify Dashboard, providing a seamless testing experience.