fs.statSync 不是函数
fs.statSync is not a function
节点版本:10.16.3
我很困惑。显然 fs.statSync
在下面的代码中不是一个函数...有人可以向我解释为什么下面的代码会抛出这个错误吗?
fs.readFile('./config.json', 'utf8',
(error, config) => {
console.log(`1. ${config}`)
if (error) console.error(`Error: ${error}`)
else {
for (const archive of JSON.parse(config).archives){
console.log(`2. ${archive}`)
console.log(`3. ${fs.statSync(archive).isDirectory()}`)
}
}
}
)
控制台
1. {
"archives": [
"C:\Windows",
"C:\AMD",
"C:\MSOCache",
"C:\PerfLogs",
"C:\Program Files",
"C:\Program Files (x86)",
"C:\ProgramData",
"C:\Users",
"C:\WebDrivers"
]
}
2. C:\Windows
3. Uncaught TypeError: fs.statSync is not a function
at file-system.js:128
at FSReqCallback.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:61)
看起来像你的承诺 fs
。它不会承诺同步方法。
无论如何,您很少想使用 Sync
方法,它们会阻塞您的事件循环。
节点版本:10.16.3
我很困惑。显然 fs.statSync
在下面的代码中不是一个函数...有人可以向我解释为什么下面的代码会抛出这个错误吗?
fs.readFile('./config.json', 'utf8',
(error, config) => {
console.log(`1. ${config}`)
if (error) console.error(`Error: ${error}`)
else {
for (const archive of JSON.parse(config).archives){
console.log(`2. ${archive}`)
console.log(`3. ${fs.statSync(archive).isDirectory()}`)
}
}
}
)
控制台
1. {
"archives": [
"C:\Windows",
"C:\AMD",
"C:\MSOCache",
"C:\PerfLogs",
"C:\Program Files",
"C:\Program Files (x86)",
"C:\ProgramData",
"C:\Users",
"C:\WebDrivers"
]
}
2. C:\Windows
3. Uncaught TypeError: fs.statSync is not a function
at file-system.js:128
at FSReqCallback.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:61)
看起来像你的承诺 fs
。它不会承诺同步方法。
无论如何,您很少想使用 Sync
方法,它们会阻塞您的事件循环。