Hmahwish
Hmahwish

Reputation: 2232

fs.existsSync is not a function in node error

I am getting this when running node in Ubuntu 14.04, 'fs.existsSync' is not a function . the code is

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

The version of node is v0.10.25 . Am I missing something.

Upvotes: 2

Views: 24138

Answers (1)

Maen
Maen

Reputation: 10698

You might have forget to require the module fs :

var fs = require("fs")

Anyway, you'd better use openSync and handle the error if the file doesn't exists :

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.

Upvotes: 3

Related Questions