How to use the linkSync function from fs-extra

Find comprehensive JavaScript fs-extra.linkSync code examples handpicked from public code repositorys.

The fs-extra.linkSync method creates a hard link synchronously.

7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
    throw err
  }


  const dir = path.dirname(dstpath)
  const dirExists = fs.existsSync(dir)
  if (dirExists) return fs.linkSync(srcpath, dstpath)
  mkdir.mkdirsSync(dir)


  return fs.linkSync(srcpath, dstpath)
}
fork icon0
star icon1
watch icon0

+ 26 other calls in file

1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
const linkPath = path.join(tempProjDir, modelFileName);

if (useLinks) {
    try {
        if (onWindows) {
            fs.linkSync(fullModelPath, linkPath);
        } else {
            fs.symlinkSync(fullModelPath, linkPath, 'file');
        }
    } catch(err) {
fork icon0
star icon0
watch icon1

+ 14 other calls in file

How does fs-extra.linkSync work?

The fs-extra.linkSync method is used to create a hard link synchronously. Hard links are links between files that share the same inode on the file system, meaning they share the same file content and metadata. The method takes two arguments: the path of the existing file to link and the path of the new hard link to create. The new path must not already exist and the original file must exist. The method creates a new hard link to the original file and updates the inode reference count for the file. If the original file is deleted, the hard link will still reference the original file's data and metadata. If the hard link is deleted, it will decrement the reference count of the original file, but the original file will remain intact as long as other hard links or the original file itself still exist.

343
344
345
346
347
348
349
350
351
352
353
        fs.ensureDirSync(dest);
        fs.readdirSync(src).forEach(entry => {
            linkFileOrDirTree(path.join(src, entry), path.join(dest, entry));
        });
    } else {
        fs.linkSync(src, dest);
    }
}


// checks if file exists and then deletes. Error if doesn't exist
fork icon0
star icon0
watch icon0

Ai Example

1
2
3
4
5
6
7
8
9
10
const fse = require("fs-extra");

try {
  // create a symbolic link to a file
  fse.linkSync("/path/to/file", "/path/to/link");

  console.log("Symbolic link created successfully!");
} catch (err) {
  console.error(err);
}

In the example above, fs-extra is first imported into the script. Then, fse.linkSync is called with two arguments: the path to the file to link to, and the path to the new symbolic link. The function will create the link, and if successful, print out a success message. If an error occurs, it will be caught and printed to the console.

function icon

fs-extra.readFileSync is the most popular function in fs-extra (9724 examples)