node.js - how to get the OS platforms user data folder

Try the following:

process.env.APPDATA || (process.platform == 'darwin' ? process.env.HOME + '/Library/Preferences' : process.env.HOME + "/.local/share")

The expected result is:

OS X - '/Users/user/Library/Preferences'

Windows 8 - 'C:\Users\user\AppData\Roaming'

Windows XP - 'C:\Documents and Settings\user\Application Data'

Linux - '/home/user/.local/share'


You can check user environment which is stored in process.env

Also, take a look at process.platform

To be specific:

% node                                                                                                                                 
> console.log(process.env.HOME)
/Users/miktam
> console.log(process.platform)
darwin

Having this information, you will be able to achieve what you need.

Tags:

Node.Js