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

删除回忆录丶 提交于 2019-12-03 06:29:58

问题


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?


回答1:


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?




回答2:


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



回答3:


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



回答4:


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


来源:https://stackoverflow.com/questions/23609042/how-to-avoid-octal-literals-are-not-allowed-in-strict-mode-with-createwritestr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!