Reputation: 3320
Normally in node one would use a path similar to this:
../js/hereIsMyJs.js
However in mac for example (pc is different)
the path can be ~/Desktop/Ohms/js/hereIsMyJs.js
Is there any module or way to use the computer path I just presented in node? I'm using a module that requires the path to where the file should be placed. It has to work dynamic so the optimal solution would be for me just to feed it the "computer" path.
const fileName = '~/Desktop/Ohms/somewhere/here.jpg'
QRCode.toFile(fileName, 'https://example.com')
Upvotes: 0
Views: 408
Reputation: 10167
To achieve what you want, it's normal to use node's native path.resolve https://nodejs.org/api/path.html#path_path_resolve_paths
// Start from current directory, and gives absolute path valid to the current OS.
console.log( path.resolve("../js/hereIsMyJs.js") ); // ie: C:\\projects\\js\\hereIsMyJs.js
// It also accepts multiple arguments, so you can feed it partial paths
path.resolve( "..", "js" ,"hereIsMyJs.js" ); // same result as above.
The tilde character ~
is short for the home directory in the *nix world. And works most places not windows.
In node you can use require('os').homedir()
to get the home directory
There is also __dirname
(gives absolute path of the directory containing the currently executing file) and process.cwd()
which gives the directory from where you executed your file
And finally there is path.join()
which is similar to resolve, but works for joining relative paths, and doesn't care about the current directory.
Upvotes: 1