Create relative symlinks using absolute paths in Node.JS
Option 1: Use process.chdir()
to change the current working directory of the process to projectRoot
. Then, provide relative paths to fs.symlink()
.
Option 2: Use path.relative()
or otherwise generate the relative path between your symlink and its target. Pass that relative path as the first argument to fs.symlink()
while providing an absolute path for the second argument. For example:
var relativePath = path.relative('/some-dir', '/some-dir/alice.json');
fs.symlink(relativePath, '/some-dir/foo', callback);
const path = require('path');
const fs = require('fs');
// The actual symlink entity in the file system
const source = /* absolute path of source */;
// Where the symlink should point to
const absolute_target = /* absolute path of target */;
const target = path.relative(
path.dirname(source),
absolute_target
);
fs.symlink(
target,
source,
(err) => {
}
);