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?

1
  • could you give some more context? Where is this piece of code in your app? What runs it? Commented Jan 15, 2019 at 16:59

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

9 Comments

Suggested commands give errors. I then removed + reinstalled nodejs as suggested in stackoverflow.com/questions/20711240/… Error remains, here is the dialog: C:\Users\fvila>node -pe 'require("fs").writeFile' require(fs).writeFile
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.<anonymous> ([eval]-wrapper:6:22) at Module._compile (internal/modules/cjs/loader.js:689:30) at evalScript (internal/bootstrap/node.js:587:27) ...
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"
If you run node --version, what's the exact output? Also, this thread reminds me of stackoverflow.com/questions/33337252/… -- 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.
I'd try starting with just building the basic Express server first as your back-end, see article medium.freecodecamp.org/….
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.