How to use the symlink function from graceful-fs

Find comprehensive JavaScript graceful-fs.symlink code examples handpicked from public code repositorys.

graceful-fs.symlink creates a new symbolic link at the specified path with the specified target path.

193
194
195
196
197
198
199
200
201
202
    })
  })
}

function makeLink (linkPath, target) {
  fs.symlink(linkPath, target, function (err) {
    if (err) return onError(err)
    return doneOne()
  })
}
fork icon1
star icon0
watch icon0

29
30
31
32
33
34
35
36
37
38
symlinkType(relative.toCwd, type, (err, type) => {
  if (err) return callback(err)
  const dir = path.dirname(dstpath)
  pathExists(dir, (err, dirExists) => {
    if (err) return callback(err)
    if (dirExists) return fs.symlink(srcpath, dstpath, type, callback)
    mkdirs(dir, err => {
      if (err) return callback(err)
      fs.symlink(srcpath, dstpath, type, callback)
    })
fork icon0
star icon1
watch icon1

+ 7 other calls in file

How does graceful-fs.symlink work?

graceful-fs.symlink() is a method in the graceful-fs library in Node.js that creates a symbolic link to a file or directory, similar to the fs.symlink() method, but with added robustness and graceful error handling.

100
101
102
103
104
105
106
107
108
109
fs.unlink(symlinkDestination, function (err) {
  if (err && err.code !== 'ENOENT') {
    log.verbose('python symlink', 'error when attempting to remove existing symlink')
    log.verbose('python symlink', err.stack, 'errno: ' + err.errno)
  }
  fs.symlink(python, symlinkDestination, function (err) {
    if (err) {
      log.verbose('python symlink', 'error when attempting to create Python symlink')
      log.verbose('python symlink', err.stack, 'errno: ' + err.errno)
    }
fork icon0
star icon0
watch icon1

283
284
285
286
287
288
289
290
291
292
// delete and recreate if the link already exists and overwrite is true.
if (opts.flags === 'w') {
  // TODO What happens when we call unlink with windows junctions?
  fs.unlink(destPath, onUnlink);
} else {
  fs.symlink(srcPath, destPath, opts.type, onSymlink);
}

function onUnlink(unlinkErr) {
  if (isFatalUnlinkError(unlinkErr)) {
fork icon0
star icon0
watch icon1

Ai Example

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

fs.symlink("/path/to/original", "/path/to/link", (err) => {
  if (err) {
    console.log(err);
  } else {
    console.log("Symlink created successfully");
  }
});

This code creates a symbolic link at /path/to/link that points to /path/to/original. The fs.symlink function takes three arguments: the path to the original file or directory, the path to the symlink, and a callback function that is called with an error object if an error occurs.