72

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?

6 Answers 6

73

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
2
  • 9
    Exactly my problem! This solutions workes well, thank you!
    – eisbehr
    Commented Oct 10, 2016 at 9:10
  • 2
    @538ROMEO 33 in octal = 27 in decimal = 1B in hexadecimal
    – ariel
    Commented Aug 9, 2019 at 18:57
62

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
1
  • 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
    Commented May 31, 2017 at 17:43
39

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?

1
  • +1 That does look like a valid solution, given the source (assuming they're the right sources and version and so on). Commented May 12, 2014 at 12:43
4

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

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"
    });
-1

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

2
  • 2
    This answer does not answer the OP's question.
    – Tyler2P
    Commented May 20, 2021 at 20:32
  • @Tyler2P Maybe it doesn't, but it's the first thing that came up when I googled an error and this helped me solve it. Commented Jul 13, 2022 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.