Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having trouble understanding how fs.stat() works

Tags:

node.js

stat

I'm trying to write a function that tells me is a certain path is a directory.

var fs = require('fs');
console.log("+++++++++++++++++++++++++++++++++++++++");
fs.statSync(pathname, function(err, stats) {
    console.log(stats.isDirectory());
});
console.log("+++++++++++++++++++++++++++++++++++++++");

However, it never prints the answer.

If pathname exists - it doesn't call the function. If it doesn't exists, it generates an exception: ENOENT not a file or directory. I don't want to know it pathname exists, but I want to know if it's a directory.

Can anyone help me fix it?

like image 312
Asher Saban Avatar asked Dec 20 '11 21:12

Asher Saban


People also ask

What does fs stat do?

The fs. stat() method is used to return information about the given file or directory. It returns an fs. Stat object which has several properties and methods to get details about the file or directory.

How do I check the size of a node js file?

To get the size of a file in Node. js, you can use the stat() method provided by the built-in fs module. This method works asynchronously and lists the statistics of a file at the given path. The size of the file is returned in bytes.

How do I stop a node process?

Method 1: Using ctrl+C key: When running a program of NodeJS in the console, you can close it with ctrl+C directly from the console with changing the code shown below: Method 2: Using process. exit() Function: This function tells Node. js to end the process which is running at the same time with an exit code.

What is Libuv in NodeJS?

libuv is a multi-platform C library that provides support for asynchronous I/O based on event loops. It supports epoll(4) , kqueue(2) , Windows IOCP, and Solaris event ports. It is primarily designed for use in Node. js but it is also used by other software projects.


2 Answers

You are using the synchronous version, which doesn't use a callback. It simply returns the result instead. So either use the async form fs.stat(path, callback) or use the sync form like this:

var fs = require('fs');
console.log("+++++++++++++++++++++++++++++++++++++++");
var stats = fs.statSync(pathname);
console.log(stats.isDirectory());
console.log("+++++++++++++++++++++++++++++++++++++++");
like image 146
Alex Wayne Avatar answered Sep 25 '22 01:09

Alex Wayne


How fs.stat() works ?

If you want to use a callback/async fs function, don't use the synchronous version, use fs.stat() :

var fs = require('fs');
console.log("+++++++++++++++++++++++++++++++++++++++");
fs.stat(pathname, function(err, stats) {
    console.log(stats.isDirectory());
});
console.log("+++++++++++++++++++++++++++++++++++++++");

There is more information about fs.stat(). You can get a lot of information about the main object :

fs.stat(path, function(err, stats) {
      console.log(stats)
}

Output :

{ dev: 2049,
  ino: 305352,
  mode: 16877,
  nlink: 12,
  uid: 1000,
  gid: 1000,
  rdev: 0,
  size: 4096,
  blksize: 4096,
  blocks: 8,
  atime: '2009-06-29T11:11:55Z',
  mtime: '2009-06-29T11:11:40Z',
  ctime: '2009-06-29T11:11:40Z' }

Lots of elements is often useless for us, yes. But here is the signification of all of these variables, according to this article :

  • dev: ID of the device containing the file
  • mode: file protection
  • nlink: number of hard links to the file
  • uid: user ID of the file’s owner.
  • gid: group ID of the file’s owner.
  • rdev: device ID if the file is a special file.
  • blksize: block size for file system I/O.
  • ino: File inode number. An inode is a file system data structure that stores information about a file.
  • size: file total size in bytes.
  • blocks: number of blocks allocated for the file.
  • atime: date object representing the file’s last access time.
  • mtime: date object representing the file’s last modification time.
  • ctime: date object representing the last time the file’s inode was changed.

You can also, like nodeJS documentation says, get more information like :

stats.isFile()
stats.isDirectory()
stats.isBlockDevice()
stats.isSymbolicLink() (only valid with fs.lstat())
stats.isCharacterDevice()
stats.isFIFO()
stats.isSocket()

About stats.isSymbolicLink(), there is another function than fs.stat, called fs.lstat(), and here is the difference between them :

  • stat follows symlinks. When given a path that is a symlink, it returns the stat of the target of the symlink.
  • lstat doesn't follow symlinks. When given a path that is a symlink it returns the stat of the symlink and not its target.
like image 21
Arthur Lacoste Avatar answered Sep 26 '22 01:09

Arthur Lacoste