Get the path to the current application in node-webkit - node-webkit

In node-webkit, is there any way to find the path to the current application? In node.js, you can use __dirname to find the path to the current application, but in node-webkit, the variable __dirname appears to be undefined.
The following node.js script prints the file path correctly:
console.log(__dirname)
The following node-webkit script does not print the file path correctly:
<script type = "text/javascript">
alert(__dirname);
</script>
What is the correct way to find the path to the current application in node-webkit?

The accepted answer's link is no longer available, so here is a short answer:
nw.js extract the content of your app, to a temp directory, every time you run it.
If you want to access the path where nw.js extracted your app, use process.cwd()
In other causes, where you want to access the path of your executable app, use:
var path = require('path');
var nwDir = path.dirname(process.execPath);

The answer to this question was discussed here: https://groups.google.com/d/topic/node-webkit/IwGzluFC9iU/discussion
On Windows, use "process.execPath" to see the path of the executable that launched it. Then work from there, removing the executable's filename from the path to get the folder's path (assuming your app's .nw is relative to the executable or is combined with it).
This works for me whether it is running with the zipped 'app.nw' or where 'nw.exe' and 'app.nw' are combined into one executable file (app.exe).

If you are looking for the path to the App source (i.e. the folder that contains package.json) then you can use process.cwd().
No matter what the environment's true working directory is when the node executable is launched, it will set process.cwd() to the location of the App source. If the App is contained in an archive, cwd will point to the temporary folder where the source is extracted.
Importantly, note that process.cwd() can be changed during the application run by process.chdir(newPath) and potentially by other events as well, so you might want to store the initial value at application launch.
EDIT:
Just to clarify, process.cwd() is set to the folder that contains the actual package.json file that is used by the running app. So if you have packaged your app in an archive or executable (zip, exe, nwz, nw, etc), then nw.exe will extract the project files to a temporary directory before running the app. So process.cwd() will point to that temporary folder, not the location of the original archive or executable.

this should work:
var nw = require('nw.gui'); //This line is only required for NW.js 0.12.x and below
console.log(nw.__dirname)

window.location won't work if you're loaded an external uri, but the following seems reliable regardless:
var path = require('path');
,appPath = path.dirname(require.main.filename)

Not sure when this is added, but I believe the official way to get the start up path now is:
nw.App.startPath

Related

Error when trying to read a file via relative path in .net core 3.X in production

When I'm running my project in localhost I'm able to locate the file and process it further. This is achieved with this line of code.
path = Path.Combine(Directory.GetCurrentDirectory(), "EmailTemplates\\SuccessOrderWindows10.html");
I'm able to get the full relative path C:\etc\etc\etc.. But when i push this code to production, when it reaches to this stage, it throws an error
Error One or more occurred. (Could not find a part of the path 'h:\root\home\username\www\sitename\EmailTemplates\SuccessOrderWindows10.html'.)
What I'm doing wrong? Do i have to select the files and set them to content so that it will be included in the build?
Directory.GetCurrentDirectory will get the current working directory of the application.When you push the code to production,you change the the current working directory,so the relative path of your file will change,so you need to put the file to the new path h:\root\home\username\www\sitename\EmailTemplates\SuccessOrderWindows10.html when production.Or you can use absolute path.

Images uploaded in Vue.js production mode not showing

I am a bit new to Vue.js. I am doing a social media application that allows users to upload and share images with others. I store my images in src/assets folder during development. However, when I build the project, all images are put in the dist folder. Therefore, what can I do to enable users to still upload images on production? Do I create the assets directory in the dist folder?
I have since tried different ways, including storing images on the backend. In dooing this, I reference the backend path relatively, using, for example, ../../../backend/public/assets..., and it works on development. However, when I build, the images that existed in the backend directory at the time of building are visible, however, whenever I try uploading more on production to the ../../../backend/public/assets... directory, they are uploaded successfully but are not visible (that is on production). I get an error that Cannot find module './image_name.image_extension'.
What am I doing wrong?
I have seen similar questions like this but there was no answer.
You must set your public path and change your way!!
first step to do is creating vue.config.js in your root directory, if you want to know more details, read this: https://cli.vuejs.org/config/
for example, I define prefix path for my files:
module.exports = {
publicPath:
process.env.NODE_ENV === "production" ? "/" : "/",
};
remember, It's better if you use "#" to define your paths.
for example, if you want to load a image which located in src/assets/files/img/myImage.png, you can use #/assets/files/img/myImage.png in template section for binding or script section of your .vue files!
It always help you to find correct path of your files.
and finally your way is not standard because "src/assets/..." will used for compiled scripts and styles and also your files which you want to use on your UI layout like static images. so you have to use "public/assets/..." directory to save your file, then you will see everything is going well for you.
if you have a more question or stuck solving this problem again, I'm here to fix your issues.

Apache server cannot find local file [duplicate]

I'm working on a Flask extension from which I want to create a directory in the project's root path on the file system.
Suppose we have this directory structure
/project
/app
/tests
/my_folder
manage.py
my_folder should be created dynamically by the extension, which is a test utility and wraps the application under test in the /tests directory. However, I'm struggling to determine the project's root path within my extension.
For now, I am trying to guess the path from the run file:
def root_path(self):
# Infer the root path from the run file in the project root (e.g. manage.py)
fn = getattr(sys.modules['__main__'], '__file__')
root_path = os.path.abspath(os.path.dirname(fn))
return root_path
This obviously breaks as soon as the tests are run from within the IDE instead of the manage.py. I could simply infer the project's root relative to the app or tests directory, but I don't want to make any assumptions regarding the name or structure of these directories (since multiple apps might be hosted as subpackages in a single package).
I was wondering if there is a best practice for this type of problem or an undocumented method which the Flask object provides (such as get_root_path).
app.root_path contains the root path for the application. This is determined based on the name passed to Flask. Typically, you should use the instance path (app.instance_path) not the root path, as the instance path will not be within the package code.
filename = os.path.join(app.instance_path, 'my_folder', 'my_file.txt')
app.root_path is the absolute path to the root directory containing your app code.
app.instance_path is the absolute path to the instance folder. os.path.dirname(app.instance_path) is the directory above the instance folder. During development, this is next to or the same as the root path, depending on your project layout.

How to add a config.js file into nwjs install directory?

I use node-webkit-builder to package my nwjs application.
Since the program running directory path is not same as the install directory path( the running directory path is like this: C:\Users[username]\AppData\Local\Temp\nw7388_7201 ). I don't know how to add my config.js file in the install directory, and read it in my nwjs app.
Best regards.
Checkout this: https://github.com/nwjs/nw.js/wiki/App#datapath
This is common folder for application data. With gui.App.dataPath you always know where is this folder.
If you need get know where is your executable lives, try:
var path = require('path');
var nwPath = process.execPath;
var nwDir = path.dirname(nwPath);
https://github.com/nwjs/nw.js/issues/1197

File upload path in a test

A geb test tests file upload in a grails application. According to the documentation the absolute path of the file has to be specified in the test. Is it possible to make the test a bit more portable between developers/machines by including a file in the grails source tree and then getting its absolute path from the geb test?
You may be interested in doing something like this
http://www.mkyong.com/java/how-to-get-the-filepath-of-a-file-in-java/
In the Get file path example he creates a temporal file and then gets its filePath. If you need a specific file then I recomend you to build / write on it after creating it, this way you have a User/enviroment free file to access from your app anytime.
I ran into a problem like this once, uploading a photo to a server with grails, and for test or repetitive actions I created it and added the base64 code on runtime.
Hope it helps. Good luck! :)
I just ended up using System.properties['base.dir'] to get real path to my application.
String cd = new File(".").getAbsolutePath().replace(".","");
String file = cd + "file.jpg"
println file
It's work for me, on windows an linux.
it's need for CI and local building test.