Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js - How to find the desktop path?

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.

like image 279
sundowatch Avatar asked Aug 31 '19 23:08

sundowatch


People also ask

How to get the path of current script using Node JS?

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.

How do I resolve a Node JS file to a directory?

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:

How do I use isdirectory in Node Node JS?

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.

How do I use the path segment separator in Node JS?

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.


Video Answer


2 Answers

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.

like image 158
Chuksy Avatar answered Sep 19 '22 15:09

Chuksy


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

like image 33
Robson França Avatar answered Sep 18 '22 15:09

Robson França