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

283
Views
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 answers
Answer question

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 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!