First I'd like to say thanks for taking the time to read this.
I am trying to open a JSON file that is in the following directory structure:
#--> Root Folder
--> App.exe
#--> Configuration
---> JSON File
So I used the Code:
var ConfigFile = "./Configuration/JSON.json";
Followed by:
var fs = require('fs');
var file_content = fs.readFileSync(ConfigFile);
var content = JSON.parse(file_content);
// Manipulate the Data
For some odd reason, Node-Webkit seems to be looking for the folder in a Temp Directory located in:
C:\Users\User\AppData\Local\Temp\nw9740_14956\Configuration
The file is not there, and thus in the Console I get the following Error:
Uncaught Error: ENOENT: no such file or directory, open
'C:\Users\User\AppData\Local\Temp\nw9740_14956\Configuration\JSON.json'
I am running Windows (as you can tell), and I would like for fs to pull the file from the Folder (Configuration) that is adjacent to the app.exe.
Any help is appreciated
I've only done this once, so I may be wrong, but it looks like you're bundling your app content into the exe? If you do this, node-webkit will extract the app contents into the %TEMP% folder and then run the content from there.
Try checking the command line arguments to see if arg[0] will point you to the actual node-webkit exe that's running the app. From there, you should be able to construct a path to your configuration data.
Related
I have the next web site folder structure (in short):
Root_folder
PHP_folder
index.php
Javascript_folder
app.js
Assets_folder
my_texture.jpg
..."index.php" calls "app.js" with the line:
<?php include '../Javascript_folder/app.js'; ?>
...and "app.js" calls "my_texture.jpg" with the line:
var texture = new THREE.TextureLoader().load("../Assets_folder/my_texture.jpg");
... but "my_texture.jpg" is not found.
If I put "my_texture.jpg" in the same folder of "index.php" and change the javascript path to:
var texture = new THREE.TextureLoader().load("./my_texture.jpg");
... the bitmap is found, but obviously I prefer to keep the assets in their own folder.
How could I find the bitmap respecting my current folder structure?
0K
I resolved this issue opening the Ubuntu terminal (bash/Ubuntu) within the root folder of my site:
Root_folder$ php -S localhost:4000/PHP_folder/index.php
My local server cant find the sub-folder "Assets_folder" files when I execute the server within the sub-folder "Assets_folder/PHP_folder/" with this order:
Root_folder/PHP_folder$ php -S localhost:4000/index.php
In short, using bash terminal/php local server must be initied from the root folder of the site.
Also, I want to warning that some remote servers (like the one that Im paying) doesn't have the .htaccess file.
I have to create and write the .htaccess:
DirectoryIndex PHP_folder/index.php
I am a Spanish speaking self-taught programmer and I hope I did not use incorrect terms
I am making an electron app that converts data from .txt files to Javascript arrays. This data is stored inside a folder called faces in the main directory. I also have a button in my app which, when clicked opens file explorer at the faces folder so the user can edit the .txt files. This works fine when running npm start, but then when I use electron builder to package my app, the app can no longer find the .txt files and the user cannot edit them (giving me lots of errors). Is there some way to have a folder of .txt files that the app uses to draw information from with Electron builder?
Edit
Below is the JS used:
//Import Lists from .txt files
var ears = fs.readFileSync('faces/ears.txt', 'utf8').split('\n');
var mouths = fs.readFileSync('faces/mouths.txt', 'utf8').split('\n');
var eyes = fs.readFileSync('faces/eyes.txt', 'utf8').split('\n');
//Opens faces txt docs in file explorer
function edit() {
shell.openItem(require('electron').remote.app.getAppPath() + '/faces')
}
Here is what happens when I open the packaged app (this is the win-unpacked result but the error is the same for .exe which runs with the installer):
As you can see it does not load an information and you can see it cannot find the faces folder or the .txt files.
I think the problem is that the path-joining character is different in each OS.
You can see that every slash was backslash(\) before your suffix, /faces.
Try using path module.
const path = require(path)
....
function edit() {
shell.openItem(
path.resolve(require('electron').remote.app.getAppPath(), 'faces')
)
}
The builded app is trying to write in the app folder itself. This is possible in dev, but not in production since che app became an asar archive. (app.asar). If you look at the error your app is trying to write inside app.asas, which is not a folder. So it's not possible to write inside it.
You probably want to save these kind of information not in the path where the application has been installed, but in some of the paths related with user config/preferences.
This: https://www.electronjs.org/docs/api/app#appgetpathname may help you retrive the right path.
I have a server file which sends out emails to user based on the restify API I have built. That file is under /lib directory. And when that file mail_helper.js tries to read the file ./email_templates/default-inline.html it fails. It is trying to find email_template file from the root of the node app instead of inside lib directory. The folder structure looks like this:
- root
- package.json
- index.js (requires lib/mail_helper.js)
- lib
- mail_helper.js (reads file ./email_templates/default-inline.html)
- email_templates
- default-inline.html
Running node index.js from the root directory throws the error:
Error: ENOENT: no such file or directory, open 'my-local-path\root\email-templates\default-inline.html'
How can I reference relative path properly to avoid this situation?
As mentioned in the comments by Keith,
Relative path should be written like this:
path.join(__dirname, 'email_templates', 'default-inline.html')
This, worked and fix all of my errors after replacing this at all the places in my module.
I'm trying to check whether a path in a parent directory is a file or a directory.
My file system looks like this:
files/
html/
index.html
javascript/
index.js
test/
test.jpg
Electron is started from the parent directory of files/.
My code in the index.js file:
console.log(`file://${__dirname}/../test/test.jpg`);
console.log(fs.lstatSync(`file://${__dirname}/../test/test.jpg`).isFile());
When I start Electron I get two messages in the log. The first one is the path of the picture I want to access (test.jpg). When I paste this path into my browser, the picture is shown, so it is the correct path.
But the second message is an error:
Uncaught Error: ENOENT: no such file or directory, lstat
And then the same path in single quotation marks ('').
I used to have the index.js and the index.html file in the same path from which electron is started from (parent folder of files/) and it worked, so I guess that it has problems accesing a parent folder.
How can I solve this?
The Node fs module works with file paths, not URLs, so you need to pass a valid path to fs.lstatSync:
fs.lstatSync(`${__dirname}/../test/test.jpg`).isFile()
The folder structure is:
HOME\htmlhelp\wwhelp\wwhimpl\js\scripts
HOME\auxi\
I can read a csv file (located in the HOME\htmlhelp dir) from a js file located in the HOME\htmlhelp\wwhelp\wwhimpl\js\scripts dir using the following:
var file_path="../../../../../htmlhelp/rule_mapping.csv";
However, the same js file (from the same location) cannot read the same csv file in the HOME\auxi\ directory!
Am using the path as var file_path="../../../../../auxi/rule_mapping.csv";
It throws an "NS_ERROR_DOM_BAD_URI: Access to restricted URI denied" error. I have checked the file permissions and stuff but no luck.
Any help would be appreciated.
The problem lies within:
var file_path="../../../../../htmlhelp/rule_mapping.csv";
It's not possible to traverse up to paths out of the root directory where the document is located.
Assuming you work under UNIX (Linux/Mac), one solution would be to create a symbolic link on the root directory of the document, such as (executing from the scripts directory):
ln -s ../../../../../htmlhelp/rule_mapping.csv rule_mapping.csv
And changing the file_path to:
var file_path="./rule_mapping.csv";
I hope this helps!