Can't write to file using fs on windows

Viewed 797

I have just downloaded LTS node (14.15.0 [npm 6.14.8]) on Windows 10 and I cannot write to file.

If I run the following script anywhere:

var fs = require("fs");
fs.writeFileSync("./test.txt", "hello world", "utf-8");

I get the following error:

[Error: EPERM: operation not permitted, open 'C:\Users\Mehow\Documents\experiments\map-engine\data\moon\test.txt'] {
errno: -4048,
code: 'EPERM',
syscall: 'open',
path: C:\\path\\to\\my\\file\\test.txt

I've tried using the async method, I get the same error.

I've checked permissions on the folder, they look fine.

I've run the console as administrator, no change.

Oddly enough, I can make folders easily with a script like this:

fs.mkdirSync(some_directory, {recursive: true});

Anyone have any idea what else I can do?

1 Answers

Please go through the below steps:- 1).Right Click . The Folder > Properties > Security Tab. Click on Edit to change Permissions > Select the user and give Full Control to that user 2).Uncheck the readonly option in the file properties

Related