Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fs.exists method doesnt exist anymore in node.js

Tags:

node.js

Im using node v0.6.12

This is my code:

var fs = require("fs");

fs.exists(".", function() {
    console.log("Whatever);
});

I get this output:

node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick
              ^
TypeError: Object #<Object> has no method 'exists'
    at Object.<anonymous> (/home/dbugger/Projects/nodetest/test.js:3:4)
    at Module._compile (module.js:441:26)
    at Object..js (module.js:459:10)
    at Module.load (module.js:348:32)
    at Function._load (module.js:308:12)
    at Array.0 (module.js:479:10)
    at EventEmitter._tickCallback (node.js:192:41)

Has "exists" been deprecated? What can I use then?

like image 872
Enrique Moreno Tent Avatar asked Oct 30 '12 10:10

Enrique Moreno Tent


2 Answers

You can use path.exists() but it's deprecated in the latest version of node. The preferred api is fs.exists() these days, so you'll need to be prepared to switch at some point.

$ node --version
v0.8.3

$ node
> require('fs').exists
[Function]
> require('path').exists
[Function: deprecated]

The relevant docs:

  • http://nodejs.org/docs/v0.6.12/api/all.html
  • http://nodejs.org/api/all.html
like image 185
broofa Avatar answered Oct 12 '22 00:10

broofa


What is your node version? I get the same result on my machine (v0.6.14). I think the exists() method has been moved from the path module to the fs module recently. Try path.exists()

like image 44
Victor Stanciu Avatar answered Oct 12 '22 01:10

Victor Stanciu