Deniz
Deniz

Reputation: 1503

how to install node's file system in a vanilla project

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');

Upvotes: 0

Views: 2092

Answers (1)

Quentin
Quentin

Reputation: 943099

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.

Upvotes: 2

Related Questions