0

I am learning how to write to a file using Nodejs. I tried the below posted example, but I received the below posted error

How can I fix this error?

Code:

var fs = require('fs');

fs.writeFile("c://NodeTest", "Hey there!", function(err) {
if(err) {
    return console.log(err);
}

console.log("The file was saved!");
});

Error:

{ [Error: EPERM: operation not permitted, open 'c:\NodeTest']
errno: -4048,
code: 'EPERM',
syscall: 'open',
path: 'c:\\NodeTest' }
halfer
  • 19,824
  • 17
  • 99
  • 186
Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • Possible duplicate of [Writing files in Node.js](https://stackoverflow.com/questions/2496710/writing-files-in-node-js) – Abraham Aug 17 '18 at 15:32

2 Answers2

0

Since it looks like you are using windows, this seems to be a file permissions error. Try running your code as administrator (e.g. right click on the dos command prompt icon and choose "Run as administrator", then try to run your script).

dcp
  • 54,410
  • 22
  • 144
  • 164
  • Downvoter, care to explain? I just tried this on my box and it worked. – dcp Aug 17 '18 at 15:47
  • Not downvoter, but I think this is bad advice in general. "I'm unfamiliar" being met with "add sudo" or similar feels like a recipe for problems. – Fissure King Aug 17 '18 at 15:54
  • @Fissure King - Thanks for the feedback. I gave a windows specific answer because the OP used "c://" in the file path, which was a clear indicator they were on windows. Based on your feedback, however, I edited the answer to reflect that. – dcp Aug 17 '18 at 16:01
0

You don't have permission to access that file. Try running as administrator or manually changing the permissions on the file

Prodigle
  • 1,757
  • 12
  • 23