0

I'm trying to write to a file on the server side, using react with create-react-app.

First I tried to use the fs module, with the following code (copy-pasted from the doc, except the console log):

const fs = require('fs');
console.log("fs=",fs);
fs.writeFile('acme.js', 'console.log("hello")', function (err) {
  if (err) throw err;
  console.log('New file acme.js is either created or if exists then updated');
});

The fs object exists, but I get the error TypeError: fs.writeFile is not a function

Then tried to use the file-system module. Here's what I entered:

var fs = require('file-system');
fs.writeFile('path/test.txt', 'aaa', function(err) {"error error"})

Then I get the error TypeError: fs.existsSync is not a function

I also tried a github repository at https://github.com/hurkanyakay/reactjs-nodejs-rest-example whose description looked like what I am trying to do. I get messages when executing the commands suggested on the Frontend, specifically Error: Can't resolve 'glamor/lib/hash' .

There appear to be existing issues on these repositories, I have added the detailed error messages there.

I'm using Windows 10 1803, npm v6.4.1, node v8.12.0

I'm wondering whether there isn't something I've missed, maybe my understanding is lacking? Am I using the wrong modules? Is it just that what I'm trying to do can't be done?

Francis
  • 702
  • 9
  • 21

1 Answers1

1

Just to be sure: your first example is a .js file you are running with node, right?

From your command prompt, try:

> node -pe 'require("fs").writeFile'

You should receive the output:

[Function]

Then try:

> node -e 'require("fs").writeFileSync("hello.txt", "Hello World", "utf8")'

This should create the file hello.txt.

If those work, then take your first example program, put it in a file called example.js, and run it with:

> node example.js

If these commands don't work correctly, you may have something seriously broken in your node install -- I'd suggest uninstalling completely and starting over.

Elliot Nelson
  • 11,371
  • 3
  • 30
  • 44
  • Suggested commands give errors. I then removed + reinstalled nodejs as suggested in https://stackoverflow.com/questions/20711240/how-to-completely-remove-node-js-from-windows Error remains, here is the dialog: C:\Users\fvila>node -pe 'require("fs").writeFile' require(fs).writeFile – Francis Jan 16 '19 at 15:32
  • C:\Users\fvila>node -e 'require("fs").writeFileSync("hello.txt", "Hello World", "utf8")' [eval]:1 'require(fs).writeFileSync(hello.txt, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ SyntaxError: Invalid or unexpected token at new Script (vm.js:79:7) at createScript (vm.js:251:10) at Object.runInThisContext (vm.js:303:10) at Object. ([eval]-wrapper:6:22) at Module._compile (internal/modules/cjs/loader.js:689:30) at evalScript (internal/bootstrap/node.js:587:27) ... – Francis Jan 16 '19 at 15:33
  • Sorry responses are scrambled in comments. Anyway, I've unistalled / reinstalled nodejs, version is now 1.15.0, the commands you suggest still give errors. It stops at the comma after "hello.txt" – Francis Jan 16 '19 at 15:37
  • If you run `node --version`, what's the exact output? Also, this thread reminds me of https://stackoverflow.com/questions/33337252/file-system-nodejs-uncaught-typeerror-fs-writefilesync-is-not-a-function?rq=1 -- is it possible you are trying to run this code _in a browser?_ If so, be aware that `fs` is not available in the browser. – Elliot Nelson Jan 16 '19 at 15:47
  • 1) "In a browser?": It's in React, it's a web app, so yes, the error does appear in the browser. 2) I believe the solution suggested in the link will save the file on the client side? Is there any way of saving on the server side? I want multiple contributors to edit the content of a data file. – Francis Jan 17 '19 at 08:01
  • Reading further into the matter, I'm gathering that to write into a server file from a react web application, you need: 1) a server module such as Express, to respond to server requests, 2) an API module such as Axios, to make those calls from the web application. https://medium.freecodecamp.org/how-to-create-file-upload-with-react-and-node-2aa3f9aab3f0 seems to describe this, from the angle of uploading files. – Francis Jan 17 '19 at 08:38
  • Exactly right. Most web applications that have any kind of state (like user logins, shared files, etc.) have both a _front-end_ and a _back-end_. The front-end is what the user sees in the browser (a "React app" is an example of a front-end). The back-end, which can be Express, is what would run in node.js. The front-end then talks to the back-end as the user uses the application, and the back-end may read/write files on a server, save records in a database like MySQL, etc. – Elliot Nelson Jan 17 '19 at 11:09
  • 1
    I'd try starting with just building the basic Express server first as your back-end, see article https://medium.freecodecamp.org/how-to-make-create-react-app-work-with-a-node-backend-api-7c5c48acb1b0. – Elliot Nelson Jan 17 '19 at 11:10
  • The link to https://medium.freecodecamp.org/how-to-make-create-react-app-work-with-a-node-backend-api-7c5c48acb1b0 looks like the best answer I've seen to this specific problem, thanks @elliotnelson, I'll check it out. – Francis Jan 17 '19 at 16:12