How to use the lstat function from fs

Find comprehensive JavaScript fs.lstat code examples handpicked from public code repositorys.

fs.lstat is a method in Node.js that retrieves the file status information of a given path, such as file size and modification time, without following symbolic links.

97
98
99
100
101
102
103
104
105
106
  assert.strictEqual(err.code, 'ENOENT');
  assert.strictEqual(err.syscall, 'lstat');
  return true;
};

fs.lstat(nonexistentFile, common.mustCall(validateError));
assert.throws(
  () => fs.lstatSync(nonexistentFile),
  validateError
);
fork icon42
star icon19
watch icon0

570
571
572
573
574
575
576
577
578
579
  }
  afterMakeParent()
}

const afterMakeParent = () => {
  fs.lstat(entry.absolute, (lstatEr, st) => {
    if (st && (this.keep || this.newer && st.mtime > entry.mtime)) {
      this[SKIP](entry)
      done()
      return
fork icon3
star icon2
watch icon0

How does fs.lstat work?

The fs.lstat method is used in Node.js to retrieve information about a file or directory, such as its type (file, directory, symbolic link, etc.), size, and permissions, by performing a system call to query the file system without following symbolic links. It returns a fs.Stats object containing information about the file or directory.

249
250
251
252
253
254
255
256
257
258

async dumpRecords(records) {
  let filePath = path.resolve('../../..', process.env.DUMP_RECORDS);
  // create new (unique) file if arg is directory
  try {
    if ((await fs.lstat(filePath)).isDirectory()) {
      filePath = path.resolve(filePath, `recordDump-${new Date().toISOString()}.json`);
    }
  } catch (e) {
    null; // specified a file, which doesn't exist (which is fine)
fork icon5
star icon0
watch icon7

175
176
177
178
179
180
181
182
183
184
}
exports.delete = del;
exports.rimraf = del;
function exists(path) {
    return new Promise(function (resolve, reject) {
        return fs.lstat(path, function (err) {
            return !err ? resolve(true) : err.code === 'ENOENT' ? resolve(false) : reject(err);
        });
    });
}
fork icon3
star icon0
watch icon1

+ 5 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
const fs = require("fs");

fs.lstat("/path/to/myfile.txt", (err, stats) => {
  if (err) {
    console.error(err);
    return;
  }

  console.log(`File size: ${stats.size} bytes`);
  console.log(`Last modified: ${stats.mtime}`);
});

In this example, fs.lstat is used to retrieve information about the file located at /path/to/myfile.txt. The stats parameter passed to the callback function contains information such as the size of the file and the last time it was modified. This information is then logged to the console.

44
45
46
47
48
49
50
51
52
53
const pdf = url.parse(encodeURI("file://" + path.join(process.cwd(), "./test/pdfs/tracemonkey.pdf"))).href;
const pdfLength = 1016315;
beforeAll(done => {
  server = http.createServer((request, response) => {
    const filePath = process.cwd() + "/test/pdfs" + request.url;
    fs.lstat(filePath, (error, stat) => {
      if (error) {
        response.writeHead(404);
        response.end(`File ${request.url} not found!`);
        return;
fork icon0
star icon0
watch icon1

440
441
442
443
444
445
446
447
448
449

if (fileUriRegex.test(this._url.href)) {
  path = path.replace(/^\//, "");
}

fs.lstat(path, (error, stat) => {
  if (error) {
    if (error.code === "ENOENT") {
      error = new _util.MissingPDFException(`Missing PDF "${path}".`);
    }
fork icon0
star icon0
watch icon1

7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
base = m[0];
previous = '';

// On windows, check that the root exists. On unix there is no need.
if (isWindows && !knownHard[base]) {
  fs.lstat(base, function(err) {
    if (err) return cb(err);
    knownHard[base] = true;
    LOOP();
  });
fork icon0
star icon0
watch icon1

330
331
332
333
334
335
336
337
338
339
340
  return extract
}


function validate (fs, name, root, cb) {
  if (name === root) return cb(null, true)
  fs.lstat(name, function (err, st) {
    if (err && err.code !== 'ENOENT') return cb(err)
    if (err || st.isDirectory()) return validate(fs, path.join(name, '..'), root, cb)
    cb(null, false)
  })
fork icon0
star icon0
watch icon0

147
148
149
150
151
152
153
154
155
156
157
}


function lstat() {
  const fs = require('fs');
  fs.writeFileSync('fs15.txt', '123', 'utf8');
  fs.lstat('fs15.txt', () => {
    fs.unlinkSync('fs15.txt');
  });
}

fork icon0
star icon0
watch icon0

+ 2 other calls in file

34
35
36
37
38
39
40
41
42
43
  errback(err)
  return
}
names.forEach((name) => {
  const filepath = path.join(directory, name)
  fs.lstat(filepath, (err, stats) => {
    if (err) {
      process.nextTick(errback, err)
      return
    }
fork icon0
star icon0
watch icon0

31
32
33
34
35
36
37
38
39
40
  errback(err);
  return;
}
names.forEach(function (name) {
  var filepath = path.join(directory, name);
  fs.lstat(filepath, function (err, stats) {
    if (err) {
      process.nextTick(errback, err);
      return;
    }
fork icon0
star icon0
watch icon0

71
72
73
74
75
76
77
78
79
80
81
  }))
}


const chownrKid = (p, child, uid, gid, cb) => {
  if (typeof child === 'string')
    return fs.lstat(path.resolve(p, child), (er, stats) => {
      // Skip ENOENT error
      if (er)
        return cb(er.code !== 'ENOENT' ? er : null)
      stats.name = child
fork icon0
star icon0
watch icon0