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

262
Views
Carga del módulo Require.js (node-fetch) para error de contexto

Estoy creando un sitio web y tengo problemas con Require.js. Quiero requerir el paquete node-fetch :

 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()

y me sale este error:

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

Intenté usar const fetch = require(['node-fetch']) , pero recibí otro error:

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

Esta es mi etiqueta de script HTML: <script data-main="script.js" src="require.js">

¿Cómo puedo arreglar esto y hacer que funcione?

Yo uso node-fetch@2.6.6

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

No puede cargar node-fetch usando Require.JS.

Require.js es un cargador de módulos para módulos AMD .

node-fetch es un módulo Common.JS (que también depende de las API proporcionadas por Node.js que no están disponibles en los navegadores).

Los módulos AMD y Common.JS son formatos de módulos muy diferentes. Sus cargadores no son compatibles incluso si ambos usan una función llamada require (usan diferentes funciones llamadas require ).

(A partir de la versión 3, node-fetch se convirtió en un módulo ECMAScript, pero aún depende de Node.js y aún no es compatible con Require.JS).


Utilice la API Fetch nativa del navegador en su lugar. node-fetch es una biblioteca diseñada para ser compatible con ella para que pueda usar la misma API en navegadores y Node.js.

about 4 years ago · Santiago Trujillo Report
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!