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
how to avoid "Octal literals are not allowed in strict mode" with createWriteStream
提问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
回答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"
});

