Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change file owner in Node.js

Tags:

node.js

fs

I would like to change the file owner of a Node.js-written file. I think there is no way to set the owner directly during file write (or maybe there's a way not documented?). Is there a way to change the owner afterwards?

My Node.js app runs in a docker container under the root user. So all files written by Node.js are owned by root. I want to set the owner to www-data, for example. Is it possible?

like image 881
CordlessWool Avatar asked Jan 23 '26 17:01

CordlessWool


1 Answers

You could use the fs module:

fs.chown(path, 1,1, console.log);

Though this presumes you know the uid and gid of www-data (user) and www-data (group).

like image 61
Paul Avatar answered Jan 26 '26 10:01

Paul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!