67

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?

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
guy mograbi
  • 27,391
  • 16
  • 83
  • 122

6 Answers6

72

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
ariel
  • 15,620
  • 12
  • 61
  • 73
61

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
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • 1
    I like this answer because it uses an integer type which makes it better for things like fs.chmod or fs.mkdir, which call for integers on the mode. Even if they "support" strings, it's a good idea to pass them the data type they expect. – Michael May 31 '17 at 17:43
38

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?

gog
  • 10,367
  • 2
  • 24
  • 38
3

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

Unmitigated
  • 76,500
  • 11
  • 62
  • 80
0

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"
    });
VIKAS KOHLI
  • 8,164
  • 4
  • 50
  • 61
0

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

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
FIKRIM
  • 19
  • 4