kabal kabal - 2 months ago 18
Node.js Question

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

I am a complete novice at Node.js

I am looking for a way to get the userdata folder using node.js, that will work on both windows, and macOS

the node.js instance would be running on the user's machine

this would return the following:

C:\Documents and Settings\JohnD\Application Data - Windows XP


C:\Users\JohnD\AppData\Roaming - Windows Vista and Up


/Users/JohnD/Library/Preferences - MacOS


is this possible?

Thanks

Answer

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.

Comments