How to use the realpathSync function from fs

Find comprehensive JavaScript fs.realpathSync code examples handpicked from public code repositorys.

fs.realpathSync is a function provided by the Node.js fs module that resolves a given path to its absolute path in the file system, resolving any symbolic links or relative path components.

76
77
78
79
80
81
82
83
84
85
exports.readFile = co.promisify(fs.readFile);
exports.readFileSync = fs.readFileSync;
exports.readlink = co.promisify(fs.readlink);
exports.readlinkSync = fs.readlinkSync;
exports.realpath = co.promisify(fs.realpath);
exports.realpathSync = fs.realpathSync;
exports.rename = co.promisify(fs.rename);
exports.renameSync = fs.renameSync;
exports.rmdir = co.promisify(fs.rmdir);
exports.rmdirSync = fs.rmdirSync;
fork icon22
star icon45
watch icon26

140
141
142
143
144
145
146
147
148
149
150
151
  };


  fs.realpath(nonexistentFile, common.mustCall(validateError));


  assert.throws(
    () => fs.realpathSync(nonexistentFile),
    validateError
  );
}

fork icon42
star icon19
watch icon0

+ 3 other calls in file

How does fs.realpathSync work?

fs.realpathSync works by resolving a given path to its absolute path in the file system, taking into account any symbolic links or relative path components.

When called, fs.realpathSync takes a single argument, which is the path to be resolved. The function then performs a series of operations to resolve any symbolic links or relative path components in the given path, such as converting a relative path to an absolute path or resolving a symbolic link to its target.

Once the path has been fully resolved, fs.realpathSync returns the absolute path as a string.

By using fs.realpathSync, Node.js developers can programmatically resolve file system paths to their absolute form, which can be useful for tasks like normalizing file paths, checking whether two paths refer to the same file, or resolving dependencies in a file system hierarchy.

60
61
62
63
64
65
66
67
68
69
70
71
  });
}


function getTargetLibPath(packageName, distDirName) {
  const packagePath = path.resolve(__dirname, "../node_modules", packageName);
  const realPath = fs.realpathSync(packagePath);
  return path.resolve(realPath, distDirName ?? "lib");
}


function getSourceLibPath(packageDir, distDirName) {
fork icon24
star icon13
watch icon0

26
27
28
29
30
31
32
33
34
35
class TestEntryPoint {
    constructor() {
        // Dirty signifies that new test file has been addeed, and is cleared once the entryPoint is written.
        this.dirty = false;
        this.files = new Set();
        this.dir = fs.realpathSync(os.tmpdir());
        // The `file` is a dummy, meant to allow Karma to work. But, we can't write
        // to it without causing Karma to refresh. So, we have a real file that we
        // write to, and allow esbuild to build from.
        this.file = path.join(this.dir, `${utils_1.random(16)}-bundle.js`);
fork icon0
star icon0
watch icon1

Ai Example

1
2
3
4
5
const fs = require("fs");

// resolve a path to its absolute form using fs.realpathSync
const resolvedPath = fs.realpathSync("./path/to/file.txt");
console.log(`Resolved path: ${resolvedPath}`);

In this example, we're using fs.realpathSync to resolve a given path ('./path/to/file.txt') to its absolute form. We call the function and pass the path as an argument, and then log the resulting resolved path to the console using a template literal. When we run this code, it will output the following message to the console: javascript Copy code

17
18
19
20
21
22
23
24
25
26
27
let NpmConfig = null


let dirName = __dirname
// Use fs.realpathSync to normalize the path(__dirname could be c:\.. or C:\..).
if (process.platform === 'win32') {
  dirName = fs.realpathSync.native(dirName)
}
const rootDir = path.resolve(dirName, '..', '..', '..', '..', '..')
const herondCoreDir = path.join(rootDir, 'src', 'herond')

fork icon0
star icon0
watch icon1

357
358
359
360
361
362
363
364
365
366
	throw new error.ItemNotFoundError('Certificate ' + certificate.nice_name + ' does not exists');
}

let certFiles      = fs.readdirSync(zipDirectory)
	.filter((fn) => fn.endsWith('.pem'))
	.map((fn) => fs.realpathSync(path.join(zipDirectory, fn)));
const downloadName = 'npm-' + data.id + '-' + `${Date.now()}.zip`;
const opName       = '/tmp/' + downloadName;
internalCertificate.zipFiles(certFiles, opName)
	.then(() => {
fork icon0
star icon0
watch icon154

126
127
128
129
130
131
132
133
134
135
  ) // __webpack_require__(...)(mod)
  // we set `mod = 'ejs'`
);

/** Configure functions.tsx for output as well */
const appDirectory = fs.realpathSync(
  path.join(process.cwd(), process.env.RAZZLE_APP_PATH || '')
);
const resolveApp = (relativePath) =>
  path.resolve(appDirectory, relativePath);
fork icon0
star icon0
watch icon1

590
591
592
593
594
595
596
597
598
599
600
    )
  })
}


mailServer.loadMailsFromDirectory = function () {
  const persistencePath = fs.realpathSync(mailServer.mailDir)
  fs.readdir(persistencePath, function (err, files) {
    if (err) {
      logger.error('Error during reading of the mailDir %s', persistencePath)
    } else {
fork icon0
star icon0
watch icon1

6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
realpath.monkeypatch = monkeypatch
realpath.unmonkeypatch = unmonkeypatch


var fs = require('fs')
var origRealpath = fs.realpath
var origRealpathSync = fs.realpathSync


var version = process.version
var ok = /^v[0-5]\./.test(version)
var old = require('./old.js')
fork icon0
star icon0
watch icon1

163
164
165
166
167
168
169
170
171
172
173
	}
}


function realExistingPartOfPath(pathName) {
	try {
		return fs.realpathSync.native(pathName);
	} catch (e) {
		// Unlike the native and promises versions of realpath,
		// realpathSync's error provides the first real part of the path that didn't exist,
		// so back out one directory and we've got the part that does exist.
fork icon0
star icon0
watch icon0

+ 7 other calls in file

52
53
54
55
56
57
58
59
60
61
62
63
  fs.watchFile(__filename, options, common.mustNotCall());
  fs.unwatchFile(__filename);
}


{
  fs.realpathSync(__filename, options);
  fs.realpath(__filename, options, common.mustCall(errHandler));
}


{
fork icon0
star icon0
watch icon0

55
56
57
58
59
60
61
62
63
64
65
66
assert.throws(() => {
  fs.realpath('path', options, common.mustNotCall());
}, expectedError);


assert.throws(() => {
  fs.realpathSync('path', options);
}, expectedError);


assert.throws(() => {
  fs.mkdtemp('path', options, common.mustNotCall());
fork icon0
star icon0
watch icon0