'Node.js File Permissions are set at 000 when I create a File. How can I create a file with read write permissions, or change permissions?

I am trying to create a file via Node.js, and would like to specifically use the the FS.openSync() method if possible. Currently I have a very simple class built to create the file. The class will be further developed, but I can only build on to the class after I solve my problem of; how to set the file user permissions for the file that node creates via, 'FS.writeSync() method', so that the file is accessible. After I create the file I think the permissions are set to 001, or 000, some ridiculous, and unneeded user permissions setting. I have copy-&-pasted my small class below, and further below that is the file I use to create an instance of the class, and run the method that creates the file.

  • My File Class
/** @format */

/* eslint-disable */
const fs = require('fs');
const {O_RDONLY, O_WRONLY, O_RDWR, O_CREAT, O_TRUNC, O_APPEND} = require('fs').constants;
const {Buffer} = require('buffer');
/* eslint-enable */

class TextBasedFile {
    constructor(path) {
        this.path = path;
        this.buf = Buffer.allocUnsafe(400000000); // 40MB (Should be more than enough)
    }

    create() {
        const fd = fs.openSync(this.path, O_CREAT, O_WRONLY);
        console.log('\n\n\n\nRESULT: ' + fd);
        return fd;
    }

    truncate() {
        const fd = fs.openSync(this.path, O_TRUNC);
        console.log('\n\n\n\nRESULT: ' + fd);
        return fd;
    }
}

module.exports.TextBasedFile = TextBasedFile;
  • Code I Use to Create The new Text File

/** @format */

const {TextBasedFile} = require('./unity/iof');

const sumfile = new TextBasedFile('./someFile.txt');

sumfile.create();



As you can see my setup is very simple, the only problem is I cannot access the file I created without the sudo command. I can't even access the file from my Ubuntu user. Below is a print out of all the files in my apps directory, and the user permissions for each one. You can see that the user-permissions for 'someFile.txt' is not going to work for me.

drwxrwxr-x   7 ajay ajay  4096 Oct  1 10:43 .
drwxrwxr-x   4 ajay ajay  4096 Sep 30 20:03 ..
-rwxrwxr-x   1 ajay ajay   533 Sep 30 22:16 app.js
-rwxrwxr-x   1 ajay ajay   858 Sep 30 22:52 .developerNotes.txt
-rwxrwxr-x   1 ajay ajay  1005 Sep 30 22:43 .eslintrc.json
drwxrwxr-x   8 ajay ajay  4096 Sep 30 23:38 .git
-rwxrwxr-x   1 ajay ajay   551 Oct  1 09:26 .gitignore
-rwxrwxr-x   1 ajay ajay  1075 Sep 30 20:03 LICENSE
-rwxrwxr-x   1 ajay ajay  1115 Sep 26 13:32 localhost-cert.pem
-rwxrwxr-x   1 ajay ajay  1704 Sep 26 13:32 localhost-privkey.pem
drwxrwxr-x 107 ajay ajay  4096 Sep 30 20:32 node_modules
-rwxrwxr-x   1 ajay ajay   980 Sep 30 20:31 package.json
-rwxrwxr-x   1 ajay ajay 36991 Sep 30 20:31 package-lock.json
-rwxrwxr-x   1 ajay ajay   466 Sep 30 22:11 .prettierrc
drwxrwxr-x   7 ajay ajay  4096 Sep 30 19:36 public
-rwxrwxr-x   1 ajay ajay   335 Sep 30 20:03 README.md
---------x   1 ajay ajay     0 Oct  1 10:43 someFile.txt
drwxrwxr-x   2 ajay ajay  4096 Sep 30 19:33 unity
drwxrwxr-x   2 ajay ajay  4096 Sep 30 20:05 .vscode

If anyone knows how to create a file, with permission settings that allows me to access the file and work with it from inside node, well....

...that would be awesome.



Solution 1:[1]

Not sure why, or how ---------x 1 ajay ajay 0 Oct 1 10:43 someFile.txt came about. Did you play with your umask?,

This is how to fix the permissions from within your code.

    create() {
        const fd = fs.openSync(this.path, O_CREAT, O_WRONLY);
        fs.fchmodSync(fd, 0o777);
        console.log('\n\n\n\nRESULT: ' + fd);
        return fd;
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 j D3V