Node.js stats.mode Property from fs.Stats Class
The stats.mode property is an inbuilt application programming interface of the fs.Stats class which is used to get the file type and mode as bit-field.
Syntax:
stats.mode;
Return Value: It returns a number or BigInt value that represents a bit-field that describes the file type and mode.
Below examples illustrate the use of stats.mode in Node.js:
Example 1:
const fs = require( 'fs' );
fs.stat( './filename.txt' , (err, stats) => {
if (err) throw err;
console.log( "using stat: the type and "
+ "mode bit-field of the file is "
+ stats.mode);
});
fs.lstat( './filename.txt' , (err, stats) => {
if (err) throw err;
console.log( "using lstat: the type and "
+ "mode bit-field of the file is "
+ stats.mode);
});
fs.stat( './' , (err, stats) => {
if (err) throw err;
console.log( "using stat: the type and "
+ "mode bit-field of the file is "
+ stats.mode);
});
fs.lstat( './' , (err, stats) => {
if (err) throw err;
console.log( "using lstat: the type and "
+ "mode bit-field of the file is "
+ stats.mode);
});
|
Output:
using stat: the type and mode bit-field of the file is 33206
using lstat: the type and mode bit-field of the file is 33206
using stat: the type and mode bit-field of the file is 16822
using lstat: the type and mode bit-field of the file is 16822
Example 2:
const fs = require( 'fs' ).promises;
(async() => {
const stats = await fs.stat( './filename.txt' );
console.log( "using stat synchronous: the type "
+ "and mode bit-field of the file is "
+ stats.mode);
})(). catch (console.error)
|
Output:
(node:9448) ExperimentalWarning: The fs.promises API
is experimental
using stat synchronous: the type and mode bit-field
of the file is 33206
Note: The above program will compile and run by using the node filename.js
command and use the file_path correctly.
Reference: https://nodejs.org/api/fs.html#fs_stats_mode
Last Updated :
25 Jun, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...