I am trying to fire up a video onclick that is located in a sub-directory of the app that is running. I can easily do this if I provide the entire path: 'C:\Users\App" or whatever, but would like to be able to point to the video regardless of where it is stored. If the user installs the app in a folder other than the default location, the video would turn up missing.
'..\videos\video.mp4' doesn't seem to work?
It depends exactly how you are opening the file (what functions you're using). In general, many will resolve it to the current working directory if it doesn't appear to be an absolute path (start with C:\
on Windows or /
on Linux/Mac).
The example path you gave is actually using ..
at the beginning, so it's going up one level. If you're folder structure is like this:
Then you want to use ./videos/video.mp4
(note the one .
).
You can also use path.resolve('./videos/video.mp4')
to convert the relative path to an absolute path based on the current working directory.
Do note, that the current working directory is the directory you launch it from, not where it lives. I.e., if you are in folder C:\Programs
and type node myProgram/script.js
the current working directory would be C:\Programs
, not C:\Programs\myProgram
(where your script is).
In that case, I prefer to use __dirname
, which always refers to the absolute file path of the file it's found in, combined with path.join()
to create path to something in the same folder.
For example: path.join(__dirname, 'videos/video.mp4')
will always resolve to something like C:\Programs\myProgram\videos\video.mp4
(assuming the script file is in C:\Programs\myProgram
).
That is probably the most reliable way. You can also console.log()
the path to see example where it is trying to call, and adjust accordingly.
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