0

simple setup:

my project tree/setup

I want to use the Node.js File-System to update the todos.json inside /js folder.

normally i use CLI's like Vue to setup a project and all works like a charm in the background. This time is want to understand how i can manually add a module like the fs by myself.

At the moment my console tells me: Uncaught ReferenceError: require is not defined

when doing this: const fs = require('file-system');

Deniz
  • 1,435
  • 14
  • 29
  • It seems like you don't have node installed? – Kobe Dec 05 '19 at 10:58
  • you cannot use server side technology on client side – GrafiCode Dec 05 '19 at 11:02
  • Does this answer your question? [Client on node: Uncaught ReferenceError: require is not defined](https://stackoverflow.com/questions/19059580/client-on-node-uncaught-referenceerror-require-is-not-defined) – GrafiCode Dec 05 '19 at 11:02
  • 1
    But are you running this in a _browser_? That's the only explanation for getting `Uncaught ReferenceError: require is not defined`. Because Node definitely knows what `require` is. Stop wasting your time, the `fs` module that gives access to the file system is for Node (server-side) only. Browsers will _never_ let you do that. They restrict access to the local file system for obvious security reasons. – Jeremy Thille Dec 05 '19 at 11:02
  • 1
    node is a JS runtime. It allows you to run JavaScript programs directly, without a browser environment. You write the script, save it, then run it from a command line: `node myscript.js` So here JavaScript is like any other programming language, and it has of course access to the hard drive via the `fs` module. All this however is completely separate from anything that happens inside a browser's ` –  Dec 05 '19 at 11:12

1 Answers1

2

While it is possible to get implementations of require that run client-side, you can't use the Node fs module in the browser. It has a JavaScript API but isn't written in JavaScript, it really does depend on Node.js.

If you want to update a JSON file on your server from the browser, then you'll need to write a web service to do the update (and then make an HTTP request from the browser to the webserver … typically using Ajax).

In general, you should use a real database and not a JSON file too. That way you'll already have solutions to concurrency problems.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335