What is realpath-native?
The realpath-native npm package provides a way to resolve the real path of a file or directory, similar to the native `fs.realpath` method but with better performance and additional features.
What are realpath-native's main functionalities?
Resolve Real Path
This feature allows you to resolve the real path of a given file or directory. It works asynchronously and provides a callback with the resolved path.
const realpathNative = require('realpath-native');
realpathNative('/some/path', (err, resolvedPath) => {
if (err) throw err;
console.log(resolvedPath);
});
Synchronous Real Path Resolution
This feature allows you to resolve the real path of a given file or directory synchronously. It throws an error if the path cannot be resolved.
const realpathNative = require('realpath-native');
try {
const resolvedPath = realpathNative.sync('/some/path');
console.log(resolvedPath);
} catch (err) {
console.error(err);
}
Promise-based Real Path Resolution
This feature allows you to resolve the real path of a given file or directory using Promises, making it easier to work with async/await syntax.
const realpathNative = require('realpath-native');
realpathNative.promise('/some/path')
.then(resolvedPath => console.log(resolvedPath))
.catch(err => console.error(err));
Other packages similar to realpath-native
fs-extra
fs-extra is a package that extends the native `fs` module with additional methods and features, including methods for resolving real paths. It provides both callback and promise-based APIs, similar to realpath-native.
graceful-fs
graceful-fs is a drop-in replacement for the native `fs` module that improves handling of file system operations, including real path resolution. It aims to handle errors more gracefully and provide better performance.
path-exists
path-exists is a simple package that checks if a given path exists. While it does not directly resolve real paths, it can be used in conjunction with other packages to ensure a path exists before resolving it.
realpath-native
This module is no longer necessary as all current releases of Node supports fs.realpath.native
now.
Use the system's native realpath
Node 9.3 added fs.realpath(Sync).native
. On older Nodes you have to use
process.binding
to access the same function. This module does that check for
you.
The advantage of the native realpath
over fs.realpath
is that the native one
better supports paths on Windows.
On node 8 the function uses the old fs.realpath
function.
Install
Install the module with npm
:
$ npm install realpath-native
Usage
const realpath = require('realpath-native');
realpath('some-path');
realpath.sync('some-path');
API
realpath(path)
Returns a promise for the resolved path of the input.
path
Type: string
realpath.sync(path)
Returns the resolved path of the input synchronously.
path
Type: string