Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

249
Views
How to use 'require' for node-fetch in a function

I am trying to follow this tutorial.

I am stuck at 1:04.

Ania is able to start the netlify dev and return results from her database.

I get an error with the function. I can see in Ania's github repo, that she uses the code that generates the error for me. I wish I could begin to understand why she doesn't get the same error from the same code.

The function Ania wrote uses a require statement as follows:

const fetch = require('node-fetch');

When I use this line, I get an error that says:

Error - require() of ES Module /Users/melaniemaslem/AniaKubowTutorials/netflix-datastax-clone/node_modules/node-fetch/src/index.js from /Users/AniaKubowTutorials/netflix-datastax-clone/functions/getGenres.js not supported. Instead change the require of index.js in /Users/AniaKubowTutorials/netflix-datastax-clone/functions/getGenres.js to a dynamic import() which is available in all CommonJS modules.

I tried replacing the require statement with

const fetch = import('node-fetch');

When I try that, I get an error that says:

TypeError - fetch is not a function

I have seen this post, which suggests I could replace that line with:

import fetch from 'node-fetch';

When I try that, I get an error that says:

SyntaxError - Cannot use import statement outside a module

That same SO post has an answer with a fall back suggestion of using something out of date. It links to this page for advice. I don't know what to make of this or how to apply it to Ania's tutorial.

Can anyone see a way to use require in a function?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!