how to avoid "Octal literals are not allowed in strict mode" with createWriteStream

StackOverflow https://stackoverflow.com/questions/23609042

  •  20-07-2023
  •  | 
  •  

Domanda

I have the following code

fs.createWriteStream( fileName, {
  flags: 'a',
  encoding: 'utf8',
  mode: 0644
});

I get a lint error

Octal literals are not allowed in strict mode.

What is the correct way to do this code so I won't get a lint error?

È stato utile?

Soluzione 3

I don't have a node installation at hand, but looking at sources it seems that they allow strings as well:

  mode     : '0644'

Does it work?

Altri suggerimenti

I came through this problem while using it in a scape squence:

console.log('\033c'); // Clear screen

All i had to do was convert it to Hex

console.log('\x1Bc'); // Clear screen

You can write them like this :

 mode     : parseInt('0644',8)

In node and in modern browsers (see compatibility), you can use octal literals:

 mode     : 0o644

You can use the 0o prefix for octal numbers instead.

let x = 0o50;
console.log(x); //40

See also https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Deprecated_octal

You can avoid this problem by using mode into string type.

1st Method

 let mode = "0766";
 fs.createWriteStream( fileName, {
        flags    : 'a',
        encoding : 'utf8',
        mode     : mode
    });

or

2nd Method

 fs.createWriteStream( fileName, {
        flags    : 'a',
        encoding : 'utf8',
        mode     : "0766"
    });

I came across this problem too

function getFirst(arr) {
    return arr[0]
}
let first = getFirst([10, 'hello', 96, 02])
console.log(first)

This is what I did to fix it

function getFirst(arr) {
    return arr[0]
}
let first = getFirst([10, 'hello', 96, '02'])
console.log(first);

apparently it doesn't accept 0 as a start of number unless it's a string

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top