Relative path readFile error ENOENT: no such file - javascript

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.

Related

How to deal with require() files inside a node_module?

I am quite new to node_moduels.
For instance, say you have this structure:
The app directory
your_app/node_modules/your_module
your_app/app.js
The module directory
your_module/index.js
lib/a_file_you_require_in_index.js
When you require the file inside the index.js. How do you make sure the path is correct, when the code is executed inside the app.js file, where the module is required?
your_module is a npm module

Webpack Including JavaScript File via <script> Tag

I'm using Webpack with Storybook and need to include a JavaScript library file in an HTML file via a script tag. Unfortunately, I get a 404 error indicating that the file was not found (the file's path is based on my local filesystem and is correct). How do I inform Webpack regarding where to look for my library or "static" files?
Add files in entry object in webpack.config.js
entryPoint = {
app: ['path/to/script', 'path/to/script2','./index.js'],
};
This will make sure that you have the library loaded when you have run index.js (entry point of project).
More on Webpack entry points.

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

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

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.

Categories

Resources