What are fs constants in Node.js?

Viewed 2108
1 Answers

It's essentially an enum - an object whose properties describe (and whose values uniquely identify) a particular flag or indicator used with fs. See the docs.

Returns an object containing commonly used constants for file system operations.

You can use them in combination with fs operations to (reasonably) concisely and readably describe what you want to do.

For example, with the fs.access from your link:

mode: It is an integer value that denotes the permission to be tested for. The logical OR operator can be used to seperate multiple permission. It can have the values fs.constants.F_OK, fs.constants.R_OK, fs.constants.W_OK and fs.constants.X_OK. It is an optional parameter. The default value is fs.constants.F_OK.

The actual values contained inside the object don't really matter (other than that they're all distinct from each other, and that they can be used with | for a bitmask).

enter image description here

Related