What is touch?
The 'touch' npm package is used to create and update the access and modification times of files, similar to the Unix touch command.
What are touch's main functionalities?
Create a new file
This feature allows you to create a new file named 'newfile.txt'. If the file already exists, it updates the access and modification times.
const touch = require('touch');
touch('newfile.txt', function(err) {
if (err) throw err;
console.log('File created');
});
Update access and modification times
This feature updates the access and modification times of an existing file named 'existingfile.txt' to the current date and time.
const touch = require('touch');
touch('existingfile.txt', { time: new Date() }, function(err) {
if (err) throw err;
console.log('Timestamps updated');
});
Force create a file
This feature forces the creation of a file named 'forcefile.txt' even if the parent directory does not exist.
const touch = require('touch');
touch('forcefile.txt', { force: true }, function(err) {
if (err) throw err;
console.log('File created or updated with force');
});
Other packages similar to touch
fs-extra
The 'fs-extra' package provides additional file system methods that aren't included in the native 'fs' module. It includes methods for creating and updating files, similar to 'touch', but also offers a wider range of file system operations like copying, moving, and removing files and directories.
node-fs
The 'node-fs' package extends the native 'fs' module with additional file system methods. It includes functionality for creating and updating files, similar to 'touch', but also provides methods for recursive directory creation and symbolic link management.
file-system
The 'file-system' package is a simple wrapper around the native 'fs' module that provides additional convenience methods for file operations. It includes methods for creating and updating files, similar to 'touch', but also offers methods for reading and writing JSON files and working with directories.
node-touch
For all your node touching needs.
CLI Usage:
See man touch
API Usage:
var touch = require("touch")
Gives you the following functions:
touch(filename, options, cb)
touch.sync(filename, options)
touch.ftouch(fd, options, cb)
touch.ftouchSync(fd, options)
Options
force
like touch -f
Booleantime
like touch -t <date>
Can be a Date object, or any parseable
Date string, or epoch ms number.atime
like touch -a
Can be either a Boolean, or a Date.mtime
like touch -m
Can be either a Boolean, or a Date.ref
like touch -r <file>
Must be path to a file.nocreate
like touch -c
Boolean
If neither atime
nor mtime
are set, then both values are set. If
one of them is set, then the other is not.