how to use the utimessync function from fs-extra in javascript

To use the utimesSync() function from the fs-extra package in Node.js, first install the package via npm by running:

index.tsx
npm install fs-extra
21 chars
2 lines

Then, you can import the package and use the utimesSync() method as follows:

index.tsx
const fs = require('fs-extra');

// Path to file
const filePath = '/path/to/my/file.txt';

// Get the current time
const now = new Date();

// Set the file's access and modified timestamps to now
fs.utimesSync(filePath, now, now);
231 chars
11 lines

The utimesSync() method takes in three arguments: the path to the file, the new access time (set to now in the example above), and the new modified time (also set to now). This function sets the file's timestamps to the provided values.

Note: this function can only change the timestamp of a file, not create one. If the specified file does not exist it will throw a ENOENT error.

gistlibby LogSnag