0

here i want to use fs library at the js file to create and edit a json file. but whenever i include this namespace an strange error comes up. if i use

var fs = require("fs")

it throws the error:

Uncaught ReferenceError: require is not defined

if i use (in terms of any order)

import fs from "fs"

then it throws

Uncaught SyntaxError: import declarations may only appear at top level of a module

i even have tried using the require.js file but in that case it throws those errors:

Uncaught Error: Module name "fs" has not been loaded yet for context: _. Use require([])

now if i use require(["fs"]) it says:

Uncaught SyntaxError: expected expression, got '<'

and a few seconds later

Uncaught Error: Load timeout for modules: fs

i even have tried to create another js file in the separate folder and use the require in that too but it still doesn't work.

  • 1
    `require()` is node.js stuff, also its `fs` module. You are trying to do this in a browser. It won't work. – tevemadar Jun 30 '22 at 08:42
  • no i am running in the back-end. how do you guys pay difference between front-end and back-end? in my view back-end is somewhere that you are doing stuffs like php, server, database and etc, and in the front-end you are using css, html,js and almost whatever seems colory and beautiful. am i getting something wrong? – mohammadhosseinborhani Jun 30 '22 at 08:55
  • Back-end is anything what happens on the server, front-end is anything what the browser processes. If you see the message `Uncaught ReferenceError: require is not defined` in the JS console of a browser, which you probably do, the code containing the `require()` call somehow reached the browser, even if you planned something else to happen. – tevemadar Jun 30 '22 at 09:55

0 Answers0