简体   繁体   中英

How to write to a file using Nodejs?

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' }

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM