0

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?

Mel
  • 2,481
  • 26
  • 113
  • 273
  • You can use https://www.npmjs.com/package/node-fetch/v/2.6.7 instead. You can see in her [package.json](https://github.com/kubowania/netflix-clone-graphql-datastax/blob/main/package.json) that Ania is using v 2.6.1 –  Apr 18 '22 at 22:09
  • In other words, run `npm uninstall node-fetch` then `npm install node-fetch@2.6.7` –  Apr 18 '22 at 22:12

0 Answers0