NodeJS - Cant find JSON File - javascript

I'm working on a express API and i want to connect to a mysql server with this api. Settings are stored in a settings.json file. I read this using
const config = JSON.parse(require(`fs`).readFileSync('../../settings.json'));
This works if the json file is in the same directory. But in this case, the settings file is in the base directory(./settings.json) but the file from where i want it to access is: ./modules/sql/mysql.js. It doesnt work:
Error: ENOENT: no such file or directory, open '../../settings.json'
Is there a better way to access/read a json file? or what am i doing wrong?
Btw. i dont want to pass the settings as a variable. I already tried it but if possible - i want to avoid it.
File/Directory strcture

did you not miss a ../ there?
try replace '../../settings.json' for '../../../settings.json'
btw, its better to use the __dirname approach, but it should work just fine without that too :)

Related

Json "require" converted to Module "require" when installing packages

So... Lets say you have this library you want to post to NPM.
You convert it to a module type and use:
import {createRequire} from "module";
const require = createRequire(import.meta.url);
To implement require onto it. As you may know require can be used to read JSONs. So you try to read a json. Everything works fine when you test it onto a local machine. When you try linking it tho and you try to call that function it throws:
Error: Cannot find module 'something.json'
Is this an on going npm issue and if so how can i avoid it? Is there another way to read JSONs other than reading them as a txt file (which isn't very practical)?

How to read cert files from Cloud Functions for Firebase Folder

I am trying to read a file under certs folder as shown below:
-functions
|
--certs
|
---idp-public-cert.perm
Here is the code I am using to read the file:
fs.readFileSync(path.join(__dirname, 'certs/idp-public-cert.pem')).
When I run the function, I get the following errors:
Error: ENOENT: no such file or directory, open '/srv/lib/certs/idp-public-cert.pem'
I would appreciate any crew on how to do this. Thanks in advance.
Just read the file using the relative path certs/idp-public-cert.pem. All relative paths will be interpreted relative to your deployed functions folder. Don't try to build a full path to the file, as you shouldn't assume where your code deployment is effectively mounted in the server instance.
I found that specifying path with "./" or "/" is iffy - because it takes a relative patch from somewhere, maybe OLDPWD or PWD, not sure how it messes up
But I found that the right way to do fix is to add
__dirname to the path. __dirname will give the path of the functions directory.
so if your function is in /home/username/firebase/functions/routes, then __dirname will show you that.
So if you have a file /home/username/firebase/abc/, then you would have to specify:
fs.readFileSync(__dirname+"../abc/"
Hope this saves the time of others!! I wasted so much time on this, it was quite frustrating, and was kicking myself for not reading the doc!

Convert the ssd_mobilenet_v1_coco model with tensorflow_js

I want to build an app with javascript which integrates object-detection. For this, I wanna use the ssd_mobilenet_v1_coco model and use it in tensorflow.
However this line of code:
C:\Users\Jonas\AppData\Roaming\Python\Python36\Scripts\tensorflowjs_converter --input_format=tf_saved_model --output_node_names='image_tensor, detection_boxes, detection_scores, detection_classes, num_detections' \saved_model\saved_model \saved_model\web_model
does not work. It gives me file not found error, but the file is actually there unless I'm very dump and turned back into computer beginner.
OSError: SavedModel file does not exist at: \saved_model\saved_model
Also, I'm not quite sure about the output node names but this is secondary.
Thanks for support, hopefully I'm not totally dump :)
This might be because you are using an absolute path instead of a relative path.
On mac or linux, if you are in the directory that contains the downloaded unzipped model, you would run a command of that type :
tensorflowjs_converter --input_format=tf_saved_model --output_node_names='detection_boxes,detection_classes,detection_scores,num_detections' --saved_model_tags=serve ./ssd_mobilenet_v1_coco/saved_model ./ssd_mobilenet_v1_coco/web_model
From what i can see you are on Windows.
If you are running your command from the directory that contains the saved_model folder, you should run the following command :
C:\Users\Jonas\AppData\Roaming\Python\Python36\Scripts\tensorflowjs_converter --input_format=tf_saved_model --output_node_names='image_tensor, detection_boxes, detection_scores, detection_classes, num_detections' .\saved_model\saved_model .\saved_model\web_model

SFTP-Nodejs create folder in remote location

I had used the following node module for sftp implementation.
https://www.npmjs.com/package/ftps
My target is to create a folder in the remote location and save my file. In the see the available session( http://lftp.yar.ru/lftp-man.html) in the documentation it shows mkdir as an option. But I have no idea how to use it. Kindly help me create a folder in remote location. I am new to server development and this confuses me. Or is it possible to create a folder?
It seems to be in the documentation but not in the actual source code index.js
You will have to use another libary.
Just adding the following lines to index.js of node modules worked.
FTP.prototype.mkdir = function (directory , mode) {
return this.raw('mkdir '+mode+' '+directory)
};
The function call in the program is as follows
ftps.mkdir(path,['-p']).cd(path).addFile(file);

I'd like to reuse code from another module in my Nodejs backend

I'm using the Yeoman Generator Angular Fullstack and I'd like to reuse JS code from different directories within my server directory. I'm referencing the file that has the functions I want like:
var something = require('/.path');
I get the error: "Cannot find module" in my terminal.
I tried a number of variations of the path working through all of the levels from server level down to file level where the given functions are contained. I looked at a few tutorials:
http://www.sitepoint.com/understanding-module-exports-exports-node-js/
AND
https://www.launchacademy.com/codecabulary/learn-javascript/node/modules
I clearly missed something. Each module of my nodejs has a controller with an exports.create function. All of my code for each module is contained within my exports.create function accept for other required modules. I have no problem requiring underscore or other libraries in my Node/Bower modules by the way.
To be as detailed as can be, I expected
var something = require('./directory/directory.controller.js');
var something = require('/.path');
The path you are using is likely incorrect. Probably you want to open the file called path.js contained in the same folder of the file from which you are importing it. In order to do that, you should change the import as follows :
var something = require('./path');
./path is a relative path where . stands for current directory.
/.path is an absolute path. In this case require is importing a hidden file in the root directory. I guess is not what you want.

Categories

Resources