fs.exists方法不再存在于node.js中
我使用节点v0.6.12
这是我的代码:
var fs = require("fs"); fs.exists(".", function() { console.log("Whatever); });
我得到这个输出:
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)
“存在”已被弃用? 那我可以用什么?
您可以使用path.exists()
但在最新版本的节点中不推荐使用它。 现在最好的api是fs.exists()
,所以你需要准备在某个时刻切换。
$ node --version v0.8.3 $ node > require('fs').exists [Function] > require('path').exists [Function: deprecated]
相关文件:
什么是你的节点版本? 我的机器上获得了相同的结果(v0.6.14)。 我认为exists()
方法最近已经从path
模块移动到了fs
模块。 尝试path.exists()
好的,升级到最新版本的节点(0.8.12)解决了这个问题。 谢谢 :)
我也有同样的问题在树莓派。 因为如果我们只是跑步
sudo apt-get install nodejs npm
这不会安装最新版本的NodeJs。 为了安装最新版本的NodeJs,运行这个命令
# Note the new setup script name for Node.js v0.10 curl -sL https://deb.nodesource.com/setup_0.10 | sudo bash - # Then install with: sudo apt-get install -y nodejs
不要尝试在Raspberry Pi上安装Node.js v0.12。 有一个尚未解决的问题仍然存在( https://raspberrypi.stackexchange.com/questions/24059/node-js-v0-11-14-exits-with-illegal-instruction )
如果上面的方法不起作用,请按照这个https://learn.adafruit.com/node-embedded-development/installing-node-dot-js