dimanche 26 décembre 2021

Require.js module(node-fetch) load for contex error

I'm making a website and I'm struggling with Require.js. I want to require node-fetch package:

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

// Getting avatar of random discord user
const token = process.env.TOKEN;

async function fetchit() {
  const response = await fetch(`https://discord.com/api/v9/users/410902667318001684`, {
    headers: {
      Authorization: `Bot ${token}`
    }
  })
  if (!response.ok) throw new Error(`Error status code: ${response.status}`)
  else{
   let obj = await response.json()
   console.log(obj.avatar)
  }
  
}
fetchit()

and I get this error:

Error: Module name "node-fetch" has not been loaded yet for context: _. Use require([])
https://requirejs.org/docs/errors.html#notloaded

I tried using const fetch = require(['node-fetch']), but I got another error:

Error: Script error for "node-fetch"
https://requirejs.org/docs/errors.html#scripterror

This is my HTML script tag: <script data-main="script.js" src="require.js">

How can I fix this and get it working?

I use node-fetch@2.6.6




Aucun commentaire:

Enregistrer un commentaire