node.js 如何使用 createWriteStream 避免“严格模式下不允许使用八进制文字”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23609042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:19:36  来源:igfitidea点击:

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

node.jslint

提问by guy mograbi

I have the following code

我有以下代码

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

I get a lint error

我收到一个 lint 错误

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?

执行此代码的正确方法是什么,以免出现 lint 错误?

采纳答案by georg

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

我手头没有节点安装,但查看来源似乎也允许使用字符串:

  mode     : '0644'

Does it work?

它有效吗?

回答by Denys Séguret

You can write them like this :

你可以这样写:

 mode     : parseInt('0644',8)

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

在 node 和现代浏览器中(请参阅兼容性),您可以使用八进制文字:

 mode     : 0o644

回答by ariel

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

我在 scape 序列中使用它时遇到了这个问题:

console.log('3c'); // Clear screen

All i had to do was convert it to Hex

我所要做的就是将其转换为十六进制

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

回答by VIKAS KOHLI

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

您可以通过使用 mode into string 类型来避免此问题。

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