
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
Object-oriented API for interacting with files in node.js
npm install file-class
Include in your project, the export is a single constructor function
var File = require("file-class");
The constructor will create an object representing a file on disk.
Arguments
"utf8")null)null)var file = new File("foo.txt");
// or
var file = new File("foo.conf", {
encoding: "utf8",
parse: function (input) { /* parse and return output */ },
stringify: function (input) { /* transform and return output */ }
});
// or even
var file = new File("my-file");
file.encoding = "binary"; // can set properties after initialization
Read the entire contents of the file (via fs.readFile()) and return in a callback.
Arguments
file.read(function (err, contents) {
// err => null or Error()
// contents => string of contents (or buffer in encoding is not utf8)
});
Create the entire directory tree for this file (via mkdirp)
Arguments
file.mkdir(function (err) {
// err => null or Error()
});
Write contents to the file (via fs.writeFile())
Arguments
file.write("FOO", function (err) {
// err => null or Error()
});
Clear the contents of the file (ie. file.write("", ...))
Arguments
file.empty(function (err) {
// err => null or Error()
});
Delete the file from the filesystem (via fs.unlink(...)).
Arguments
file.unlink(function (err) {
// err => null or Error()
});
Check for this file's existence (via fs.exists(...)).
Arguments
true/falsefile.exists(function (exists) {
// exists => true/false
});
Get a fs.Stats object for the file (via fs.stat(...)).
Arguments
file.stat(function (err, stats) {
// err => null or Error()
// stats => fs.Stats object
});
The constructor will create an object representing a JSON file on disk. This object exposes helper methods for dealing with JSON.
Arguments
FileFile, with some additions:
null)null)var json = new File.JSONFile("package.json");
json.read(function (err, json) {
// json => parsed JSON object for file
});
Reads the file, uses extend to merge the data in before writing.
Arguments
json.merge({ foo: "bar" }, function (err, contents) {
// err => null or Error()
// contents => final state of file contents
});
The constructor will create an object representing a file on disk whose contents are comprised of one item per-line. This object exposes helper methods for dealing with that collection.
Arguments
FileString, RegExp, Functionvar list = new File.ListFile("banned.txt");
list.read(function (err, users) {
// users => array of users (one per line of file)
});
This property can be added via the options object in the constructor, or can
be set manually as an object property.
If a String, any line beginning with that same string will be ignored.
If a RegExp, any line that matches the regular expression will be ignored.
If a Function, any line that returns true when executing the function will be ignored.
NOTE Empty lines (this includes lines that consist only of whitespace) are always ignored.
var list = new File.ListFile("banned.txt", { ignore: "#" });
// or perhaps
list.ignore = /^#/;
// or even
list.ignore = function (line) {
return line[0] === "#";
};
list.read(function (err, users) {
// users => will exclude entries that begin with a '#' character
// any of the above methods yield the same result in this case
});
Determine the index of the item specified in the collection.
Arguments
this.read(...))list.indexOf("hello world", function (err, index, list) {
// err => null or Error()
// index => -1 or index in array
// list => the list that was read from disk
});
Determine whether or not an item is in the collection at all.
Arguments
true/falsethis.read(...))list.contains("hello world", function (err, contains, list) {
// err => null or Error()
// contains => -1 or index in array
// list => the list that was read from disk
});
Add a new item to the collection.
Arguments
list.add("hello world", function (err) {
// err => null or Error()
});
Remove an item from the collection. (either by value, or index)
Notice: this will only remove the first occurence, even if the value occurs multiple times in the file.
Arguments
Number: will remove that line via index. String: will call this.indexOf() to determine which line to remove)list.add("hello world", function (err) {
// err => null or Error()
});
FAQs
Object-oriented API for interacting with files
We found that file-class demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.