I have the following script:
var fs = require("fs");
fs.renameSync(__dirname + "\\file.txt", __dirname + "\\destination");
Simply trying to move the file file.txt into the folder destination with Node.js using the File System API.
Here is exactly what I run an as elevated Command Prompt as Administrator in Command Prompt and Windows PowerShell, the results plus the details:
Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.
C:\WINDOWS\system32>cd..
C:\Windows>cd..
C:\>cd Users
C:\Users>cd Leo
C:\Users\Leo>cd Desktop
C:\Users\Leo\Desktop>cd directory
C:\Users\Leo\Desktop\directory>takeown /F "C:\Users\Leo\Desktop\directory"
SUCCESS: The file (or folder): "C:\Users\Leo\Desktop\directory" now owned by user "PC\
Leo".
C:\Users\Leo\Desktop\directory>npm cache clean
C:\Users\Leo\Desktop\directory>node script.js
fs.js:636
return binding.rename(pathModule._makeLong(oldPath),
^
Error: EPERM, operation not permitted 'C:\Users\Leo\Desktop\directory\destination'
at Error (native)
at Object.fs.renameSync (fs.js:636:18)
at Object.<anonymous> (C:\Users\Leo\Desktop\directory\script.js:2:4)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
C:\Users\Leo\Desktop\directory>node -v
v0.12.4
C:\Users\Leo\Desktop\directory>npm -v
2.13.1
C:\Users\Leo\Desktop\directory>npm list
C:\Users\Leo\Desktop\directory
└── fs@0.0.2
I have Full Control Access (Ownership) of the related folders, subfolders and files:
C:\Users\Leo\Desktop\directory
C:\Users\Leo\Desktop\directory\script.js
C:\Users\Leo\Desktop\directory\file.txt
C:\Users\Leo\Desktop\directory\destination
C:\Users\Leo\Desktop\directory\node_modules\fs\index.js
C:\Users\Leo\Desktop\directory\node_modules\fs\package.json
Ran takeown /F of each of these successfully just in case.
Also tried alternative syntax on the file and folder paths:
fs.renameSync("C:\\Users\\Leo\\Desktop\\directory\\file.txt",
"C:\\Users\\Leo\\Desktop\\directory\\destination");
Spyware protection and Virus protection disabled.
Updated and running the latest versions of node, npm and fs.
PC is Windows 8.1 Pro x64 with latest updates.
No Outbound Firewall rules blocking node or npm.
I have Inbound Firewall rules allowing:
C:\Program Files\nodejs\node.exe
C:\Program Files\nodejs\npm
Feeling like I've exhausted all options, any ideas would be greatly appreciated.
You can only rename file to file or directory to directory, but you can't mix them, such as in your situation (file to directory).
In that case, add the name of the file to the newPath
argument:
fs.renameSync(__dirname + "\\file.txt", __dirname + "\\destination\\file.txt");