What is xdg-basedir?
The xdg-basedir package is a Node.js module that provides access to the XDG Base Directory Specification paths. It allows developers to easily find standard directories like configuration, data, and cache directories according to the XDG standards, which are commonly used in various Linux distributions.
What are xdg-basedir's main functionalities?
Access to XDG Config Directory
This feature allows developers to retrieve the path to the user's XDG config directory, which is used to store configuration files.
const xdgBasedir = require('xdg-basedir');
console.log(xdgBasedir.config);
Access to XDG Data Directory
This feature provides the path to the XDG data directory, where applications can store data files.
const xdgBasedir = require('xdg-basedir');
console.log(xdgBasedir.data);
Access to XDG Cache Directory
This feature retrieves the path to the XDG cache directory, which is intended for non-essential data files.
const xdgBasedir = require('xdg-basedir');
console.log(xdgBasedir.cache);
Other packages similar to xdg-basedir
env-paths
Similar to xdg-basedir, env-paths computes paths for storing things like data, config, cache, etc. It works across different platforms (Windows, macOS, Linux), providing a broader scope compared to xdg-basedir which primarily targets XDG standards on Unix-like systems.
appdirs
appdirs is another package that provides similar functionality to xdg-basedir, offering a simple interface to find appropriate platform-specific dirs (like those for data or config). It is also cross-platform and provides more comprehensive support for different operating systems compared to xdg-basedir.
xdg-basedir
Get XDG Base Directory paths
This package is meant for Linux. You should not use XDG on macOS or Windows. Instead, you should follow their platform conventions. You can use env-paths
for that.
Install
$ npm install xdg-basedir
Usage
import {xdgData, xdgConfig, xdgDataDirectories} from 'xdg-basedir';
console.log(xdgData);
console.log(xdgConfig);
console.log(xdgDataDirectories);
API
The exports xdgData
, xdgConfig
, xdgCache
, xdgRuntime
will return undefined
in the uncommon case that both the XDG environment variable is not set and the users home directory can't be found. You need to handle this case. A common solution is to fall back to a temporary directory.
xdgData
Directory for user-specific data files.
xdgConfig
Directory for user-specific configuration files.
xdgCache
Directory for user-specific non-essential data files.
xdgRuntime
Directory for user-specific non-essential runtime files and other file objects (such as sockets, named pipes, etc).
xdgDataDirectories
Preference-ordered array of base directories to search for data files in addition to xdgData
.
xdgConfigDirectories
Preference-ordered array of base directories to search for configuration files in addition to xdgConfig
.