Error when trying to acces parent folder in Electron/Node.js - javascript

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()

Related

module not found , falls outside of src directory error event though the folder is inside of src directory

i am importing the abi of smart contract inside one component in react js. and this error is coming up. although the contract abi "artifacts" folder is inside of the src directory. how is this error coming up possible?
Module not found: Error: You attempted to import ../../../../artifacts/contracts/cont.sol/VerifySig.json which falls outside of the project src/ directory. Relative imports outside of src/ are not supported.
You can either move it inside src/, or add a symlink to it from project's node_modules/.

Relative path readFile error ENOENT: no such file

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.

How to write a file path that will point to a directory?

How to write a file path that will point to a directory.
From what I have googled,
/path means root,
./path means current directory,
../path means parent directory
I am playing with Framer.js + Yeoman.
If you look at the screenshot attached:
myLayers = Framer.Importer.load("../imported/test1") is not working?
When I put the "imported" folder under the "app" folder it is working.
Looks like you need to hope out one more parent directory, to the root test1.framer directory, before you can go into the imported directory. ie:
myLayers= Framer.Importer.load("../../imported/test1")
(starting from main.js file and only going up one parent directory only gets you in the app directory)

Node-Webkit won't open file

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.

ajax call to a local csv file

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!

Categories

Resources