Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

289
Vistas
I cannot find a way to use 'require' in html

I am now developing JS in html, but I'd got following error: Uncaught (in promise) ReferenceError: require is not defined.

The code is just simple subs = require("./subscriptions.json").subs;.

To solve this problem, I first tried module with import and got another errors.
<script type="module" src="subscribe.js"></script>

  1. import { subs as subs } from "./subscriptions.json"; → Uncaught SyntaxError: Unexpected token '{'
  2. import subs from "./subscriptions.json"; → Uncaught SyntaxError: Unexpected identifier
  3. import * from "./subscriptions.json"; → Uncaught SyntaxError: Unexpected token '*'
  4. import "./subscriptions.json"; → Uncaught SyntaxError: Unexpected string

This was same with "fs", a JS module, so I gave it up and tried to use require.js(2.3.6). But similarly, it threw errors.
<script data-main="subscribe.js" src="require.js"></script>

  1. require(["./subscriptions.json"]); → Uncaught Error: Script error for "subscriptions.json"
  2. require(["json!./subscriptions.json"]); → Uncaught Error: Script error for "json", needed by: json!subscriptions.json_unnormalized2 , Uncaught Error: Script error for "subscriptions.json" , Uncaught Error: Load timeout for modules: json!subscriptions.json_unnormalized2
  3. require(["fs"]); → Uncaught Error: Script error for "fs"

What could be the problem in my code? When needed, I can share my full code.

ps. Actually I don't need to require or import something. I just want my independent JS files to share a json data. If there is a better way, share yours please. Thank you.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can't use require in es6 modules. require is used in node.js and to read a json file in your browser side javascript you can use the fetch api in your javascript file and can get data from .json file and use it in HTML

fetch("test.json")
    .then(response => response.json())
    .then(json => console.log(json));
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda