As the mentioned Node.js - Find home directory in platform agnostic way question, we can find the home folder via this code:
const homedir = require('os').homedir();
But how can I find the desktop folder whatever language is it in Windows? Because in windows desktop folder name is differ to languages.
How to get the path of current script using Node.js ? We can get the path of the present script in node.js by using __dirname and __filename module scope variables. __dirname: It returns the directory name of the current module in which the current script is located.
In this case Node.js will simply append /joe.txt to the current working directory. If you specify a second parameter folder, resolve will use the first as a base for the second: If the first parameter starts with a slash, that means it's an absolute path:
Node.js doesn’t expose a first-party isDirectory method on the fs module. You must build it yourself with the existing low-level APIs. Yet, Node.js provides you the needed helpers to compose an isDirectory (path) method using the fs core module.
Being part of the Node.js core, it can be used by simply requiring it: This module provides path.sep which provides the path segment separator ( \ on Windows, and / on Linux / macOS), and path.delimiter which provides the path delimiter (; on Windows, and : on Linux / macOS). Return the last portion of a path.
Try this
const homeDir = require('os').homedir(); // See: https://www.npmjs.com/package/os
const desktopDir = `${homeDir}/Desktop`;
console.log(desktopDir);
It should work on Windows and MacOS.
Actually, the name is always Desktop, regardless of language. However Windows creates a virtual alias for the folder. In Portuguese is "Área de Trabalho", but you can access with %USERPROFILE%\Desktop
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With