Netlify Functions with npm packages

14 June 2020

Let's assume that you already have an app deployed to Netlify. Now, install Netlify CLI

npm i -g netlify-cli

Create a folder for functions in your root directory and put a .js file inside

touch /functions/MyFunction/myFunction.js

Let's include subfolders in case later we'll be using more than one serverless function.

Let's also use the fact that Netlify can install packages during its build process and in the subscribe folder run:

npm init -y

This will setup package.json just for the myFunction directory and will enable us to install packages

npm i <package_name>

Because we'll be using it in our myFunction.js, which may look like this:

// some code I yet haven't written

Then, again in project root install:

npm i netlify-lambda

netlify-lambda is, as per documentation:

an optional tool that helps with building or locally developing Netlify Functions with a simple webpack/babel build step. For function folders, there is also a small utility to install function folder dependencies.

Next, add postinstall script in your root's package.json

{
  "scripts": {
    "postinstall": "netlify-lambda install",
  },
}

netlify-lambda install is a utility function for installing dependencies either locally or during the build process.

Lastly, run:

npm i

And you're all set to use your Netlify Function!

P.S. To locally test that function run:

netlify dev

This will spin up the project connected with Netlify including your functions and API keys (if you have any).