I'm renewing my discord.js bot folder hirarchy. To make in more structured I made a folder "src" with all js-files. Now if I want to readFileSync a json file outside the src-folder, I can't figure out how to do it.
Lets imagine this Folder:
Folder {
src {
commands {
ping.js
}
main.js
}
config1.json
database {
config2.json
}
}
I use this command to read a json file:
const config1 = JSON.parse(fs.readFileSync("____config1.json", "utf-8"));
const config2 = JSON.parse(fs.readFileSync("____config2.json", "utf-8"));
I hope my folder illustration was clear. Can someone help me how to access both of those files? The underscores are the missing characters
Thanks in advance!
Just like how you could use cd .. to navigate back a directory, you can do the same with fs.
const config1 = JSON.parse(fs.readFileSync("../config1.json", "utf-8"));
const config2 = JSON.parse(fs.readFileSync("../database/config2.json", "utf-8"));
../config1.json and ../database/config2.json
Related
So the thing is that, I'm storing files of other guys on my server that are mp4 but I need to make sure to delete that later on but sometimes the files remain.
So I want to do something that will detect if any .mp4 file is found in the folder if yes then I can remove it using unlinkSync.
I don't know the filename, that's why I want the system to figure it out
Hope you guys can help?
You can use readDir to read all filenames in a folder. Afterwards you can check the file ending:
const testFolder = './tests/';
const fs = require('fs');
fs.readdir(testFolder, (err, files) => {
files.forEach(file => {
console.log(file.includes(".mp4"));
});
});
so I'm trying to set up a very basic tfjs project but am stuck in an early part of getting it running. Running into a file path not found error: 'Error: ENOENT: no such file or directory'. My code looks like this:
const tf = require('#tensorflow/tfjs')
require('#tensorflow/tfjs-node')
async function test_function() {
const house_sales_dataset = tf.data.csv('file://./datasets/kc_house_data.csv');
const sample = house_sales_dataset.take(10);
try {
const dataArray = await sample.toArray();
} catch(err) {
console.log(err)
}
}
test_function();
My file structure for the npm project is the following:
Root project directory is called 'tensorflow-practice'. Inside this is app.js, package.json, package-lock.json, node-modules, and a folder called 'datasets'. Inside 'datasets' folder is the .csv file called 'kc_house_data.csv'.
https://imgur.com/a/jWLr3wt
Not sure why this isn't working...any help much appreciated.
I have a file that I am trying to make a config.json file for to be able to input and change the data quick and easily. The code below is my mention.js file that I mapped and referenced into my index.js File. The data below needs to be changed periodically and and a config file that I can set the 1. accountName and 2. userId Into my mention.js file. I’m new to npm and JavaScript and am still learning so any help and advice would be extremely helpful. Thank you!!
const map = new Map();
map.set("Pogo0303PTA",'<#679533427925450852>');
map.set("Wretched0671pta", "<#679533427925450852>");
map.set("Pogo0347PTA", "<#679533427925450852>");
map.set("Wretched0047pta", "<#679533427925450852>");
map.set("PogoDroid","<#679533427925450852>");
module.exports = map;```
```{
"acct1": "pogopta000",
"userid1": "65465132168451324"
},
{
"acct2": {account}
"userid2": {userID}
} ETC...
If you only need to read the config.json file once, you can use const config = require('./config.json'), assuming you have the .js file in the same folder as config.json.
If you need to read config.json you can use node's built in module fs by putting const fs = require('fs') at the top of your program and using let config = JSON.parse(fs.readFileSync('./config.json')) to read the file, and using fs.writeFileSync('./config.json', JSON.stringify(config)) to write the file.
You can structure your json file like:
[
{
"accountName": "Pogo0303PTA",
"userId": "<#679533427925450852>"
},
{
"accountName": "Wretched0671pta",
"userId": "<#679533427925450852>"
},
etc...
]
Hope this helped!
This is my first electron/node application, I m trying to use a json file as a datastore. so I created a simple one index.json under the app folder next to index.js|css|html
I installed a npm package jsonfile that is loading just fine
When I try to load my json file the EOF is rised claiming that there is no json file, and I can see that using the DevTools source tab that my json file is not there ( not loaded )
I tried force reload from electron app menu.
Here is my files code that is reading my json
const jsonfile = require('jsonfile')
const file = '/index.json';
var json;
jsonfile.readFile(file)
.then(obj => json = obj)
.catch(error => console.error(error))
------------ Edit
correcting the path name to index.json or ./index.json rises the same issue
You can use the native fs (filesystem) module.
let path = "index.json"
const fs = require('fs');
const json = JSON.parse(fs.readFileSync(path));
Thanks for your support
For me the issue was more about file system handling than electron.
All I did is to chmod my project folder to assure that I will be able to read and write into the index.json datastore
sudo chmod -R 777 /opt/workspaces/electron/myElectronPrpjectFolder
Then for a better path resolution I used the basic idea used in electron archtype, It more error safe
const path = require('path')
const file = path.join(__dirname,'index.json');
var json;
var html = "";// The returned object.
$(document).ready(function () {
jsonfile.readFile(file)
.then(obj => {
json = JSON.parse(JSON.stringify(obj));
console.log(JSON.stringify(json))
parseIssues(json.children);
document.getElementById('a').innerHTML = html;
})
.catch(error => console.error(error))
});
You can see that I m using JQuery in this snippet but it also works without JQuery.
in resume, better path resolve policy with granted priveleges on folder.
Thanks
My spec below clicks on a download button and then verifies the download.
The only problem in the program is that you need to know the name of the file in advance.
Rather I want it to look for a wild card in the directory like *.mp4
I have read up on glob but not sure how to implement it.
Can someone show me how to integrate it or another way, into code below? -
element(by.css('.download-recording .pgi-button-wrapper')).click();
browser.driver.wait(function() {
return fs.existsSync('/home/user,/Downloads/Recordings/ProtractorMeeting-2019-01-24T21_56_24.000Z.mp4')}, 8000).then(function() {
console.log("File Downloaded");
console.log(fs.statSync('/home/user/Downloads/Recordings/ProtractorMeeting-2019-01-24T21_56_24.000Z.mp4').size);
console.log(fs.readFileSync('/home/user/Downloads/Recordings/ProtractorMeeting-2019-01-24T21_56_24.000Z.mp4').length);
});
You can read whole directory with
fs.readdirSync
and then just filter received array of results, and generate abs path:
let fs = require('fs')
let folderfiles = fs.readdirSync('./')
console.log(folderfiles)
let mp4filePaths = folderfiles
.filter(file=> file.endsWith('.mp4'))
.map(file=> path.resolve(__dirname, file))
console.log(mp4filePaths)