gpt4 book ai didi

node.js - fs.existsSync is not a function in node 错误

转载 作者:搜寻专家 更新时间:2023-11-01 00:41:52 25 4
gpt4 key购买 nike

我在 Ubuntu 14.04 中运行 Node 时得到这个,'fs.existsSync' 不是一个函数。代码是

 var fs = require('fs');
fs.existsSync(buildDescFile);

Node 的版本是 v0.10.25 。我错过了什么吗?

最佳答案

您可能忘记要求模块 fs :

var fs = require("fs")

无论如何,你最好使用openSync如果文件不存在则处理错误:

fs.exists() is an anachronism and exists only for historical reasons. There should almost never be a reason to use it in your own code.

In particular, checking if a file exists before opening it is an anti-pattern that leaves you vulnerable to race conditions: another process may remove the file between the calls to fs.exists() and fs.open(). Just open the file and handle the error when it's not there.

关于node.js - fs.existsSync is not a function in node 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31339580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com