---
title: "How to make a fetch request using node-fetch v3 | Netlify"
description: "Realize the speed, agility and performance of a scalable, composable web architecture with Netlify. Explore the composable web platform now!"
source: "https://www.netlify.com/blog/how-to-make-a-fetch-request-using-node-fetch-v3/"
last_updated: "2026-07-10T23:59:59.000Z"
---
I’ve been playing around with [Astro](https://ntl.fyi/3PX0V1N) lately by building a Photography based portfolio site using [Pexel’s API](https://www.pexels.com/api/). During the process, I ran into an issue with getting my fetch request to work within my Netlify [Serverless Functions](https://ntl.fyi/3Pk3KK5). Upon further review, I made the discovery that [node-fetch v3](https://github.com/node-fetch/node-fetch) had a breaking change that was causing it not to work with my current setup. This post will break down the initial issue I had with node-fetch as well as what will need to be done to resolve it when adding node-fetch within Netlify Functions.

> Note: This is not an Astro-related issue, I just so happened to be working on a site using Astro when I ran into this error. This issue is specific to the new node-fetch version when using it within Netlify Functions.

## node-fetch v3 vs. v2

Let’s say you are in the process of working on a new site that requires you to pull data from an external API. You’ve installed node-fetch and you’ve already created your Netlify Function that contains your fetch request and it looks something like this:

```
const fetch = require('node-fetch')
export const handler = async () => {
  const baseUrl = '<https://api.pexels.com/v1/collections/gb7kvpf>'
  const response = await fetch(baseUrl, {
    method: 'GET',
    headers: {
    Authorization: `${process.env.PEXELS_API_TOKEN}`
  }
})
.then((res) => res.json())
.catch((err) => console.error(err))
...
```

When running [`ntl dev` or `netlify dev`](https://ntl.fyi/3Pk0PkQ) you will immediately get the error below in your console:

```
"errorMessage":"Cannot use import statement outside a module","errorType":"SyntaxError","level":"error","stackTrace":["ort fetch from 'node-fetch';","^^^","","taxError: Cannot use import statement outside a module","compileFunction (<anonymous>)"
```

This error message is letting us know that we are unable to use the `require()` syntax since it only works with CommonJS. Node-fetch v3 was updated to an ESM-only module which now makes it so that instead of using `require()`, you will now need to use imports.

Now that we know why it wasn’t working as expected, we can replace require with an import like the snippet below.

```
- const fetch = require('node-fetch')+ import fetch from 'node-fetch';
```

> Note: As of 08/22 node-fetch v2 is still compatible with CommonJS and will get the same bug fix updates as V3. If you decide you would rather stick with v2, you will just need to install it using `npm install node-fetch@2` [https://www.npmjs.com/package/node-fetch](https://www.npmjs.com/package/node-fetch)

## Update your netlify.toml

Now that you’ve updated your import, you will need to go into your [`netlify.toml`](https://ntl.fyi/3zhlD6M) file and add the below snippet:

```
  [functions]    node_bundler = "esbuild"
```

This tells Netlify that you will be using ESM for this site. Once you’ve saved your changes and re-uploaded your dev server, you should see that your fetch request is now going through.

## Or… update your package.json

Another option that is available is that instead of updating the TOML file, you can go into your package.json and above the scripts object you will need to add `"type": "module"`.

```
  "type": "module",  "scripts": {    "dev": "astro dev",    "start": "astro dev",    "build": "astro build",    "preview": "astro preview"  },
```

This update will work the same as adding the node\_bundler to your TOML file. You will also need to make sure to close out of your current server and re-run it again to see the changes that were made.

## Done!

Now that you’ve made the necessary updates to resolve the node-fetch issue on your project, you can now focus on crossing the finish line. If you want to check out the node-fetch solution on the site I made with Astro and the Pexels API, you can check it out [here.](https://github.com/taty2010/astro-portfolio-site)

### Share

-   [X (fka Twitter)](https://twitter.com/intent/tweet?text=How to make a fetch request using node-fetch v3&url=https://www.netlify.com/blog/how-to-make-a-fetch-request-using-node-fetch-v3/)
-   [LinkedIn](https://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Fwww.netlify.com%2Fblog%2Fhow-to-make-a-fetch-request-using-node-fetch-v3%2F)
-   [Facebook](https://www.facebook.com/sharer.php?u=https://www.netlify.com/blog/how-to-make-a-fetch-request-using-node-fetch-v3/)
-   [Bluesky](https://bsky.app/intent/compose?text=How to make a fetch request using node-fetch v3+https://www.netlify.com/blog/how-to-make-a-fetch-request-using-node-fetch-v3/)

* * *

### Tags

-   [Netlify Functions](/blog/tags/netlify-functions/)
-   [astro](/blog/tags/astro/)
-   [Fetch API](/blog/tags/fetch-api/)

## Keep reading

![](/_astro/3f45eb6eda4ea8814be310e3df4a7883a5bd9ba0-1200x675_ZcBDUS.webp)

Guides & Tutorials May 15, 2026

[

### How to build a real-time AI chatbot in minutes with Netlify Agent Runners (no backend)

](/blog/how-to-build-a-real-time-ai-chatbot-in-minutes-with-netlify-agent-runners-no-backend)

-   ![Profile picture of Nahrin Jalal](/_astro/f0e7c8f227a03fe58340c99ef5439d5a896c0733-272x272_Z23kDpD.webp)
    
    Nahrin Jalal
    

![](/_astro/8fe9e8a23f944c9912003233d99a2df7fee637cf-1600x900_Z1gMhmf.webp)

Guides & Tutorials May 15, 2026

[

### Tracking AI search traffic: how to use Netlify Log Drains to maximize AEO

](/blog/tracking-ai-search-traffic)

-   ![Profile picture of Nahrin Jalal](/_astro/f0e7c8f227a03fe58340c99ef5439d5a896c0733-272x272_Z23kDpD.webp)
    
    Nahrin Jalal
    

## Recent posts

News & Announcements June 25, 2026

[

### Netlify Functions, designed for Agent Experience

](/blog/netlify-functions-designed-for-agent-experience)

-   ![Profile picture of Eduardo Bouças](/_astro/52958f21e8450baf6d8e60302341a984e220c0cd-512x512_13VDlu.webp)
    
    Eduardo Bouças
    

News & Announcements June 24, 2026

[

### How we measure Netlify’s Agent Experience

](/blog/how-we-measure-netlify-agent-experience)

-   ![Profile picture of Sean Roberts](/_astro/bbf2243f8171dbddd80ab2103622106cef84d125-512x512_Z1d2LKE.webp)
    
    Sean Roberts
    

Guides & Tutorials May 15, 2026

[

### How to build a real-time AI chatbot in minutes with Netlify Agent Runners (no backend)

](/blog/how-to-build-a-real-time-ai-chatbot-in-minutes-with-netlify-agent-runners-no-backend)

-   ![Profile picture of Nahrin Jalal](/_astro/f0e7c8f227a03fe58340c99ef5439d5a896c0733-272x272_Z23kDpD.webp)
    
    Nahrin Jalal
    

![](/_astro/3f255b372fa958df35802666ee33b4609b2d71bd-1200x1586_1VtE2D.webp)

### How do the best dev and marketing teams work together?

[Access the report](https://www.netlify.com/reports/2024-leadership-trend-report/access/)