Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

285
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda