I am trying to get json values using nodejs but not working.I have searched some question in stackoverflow related this but always I am getting [Object Object] like this.I do not know Why I am getting like this.Anyone can resolve this issue?
file.json:
{
"scripts": {
"mr": "place",
"kg": "time",
"bh": "sec"
}
}
extension.js:
var fs = require("fs");
var file = JSON.parse(fs.readFileSync("c:\\xampp\\htdocs\\projects\\file.json", "utf8"));
console.log(file);
This is not duplicate. I have tried many ways but not working.
Note:I am using this code inside my visual studio code extension.
In node, you can import JSON like a JavaScript file
const file = require('./file.json')
console.log(file)
See is there a require for json in node.js for more info
const data = require("./file.json")
console.log(data.scripts)
Try this one out it is simple.
const fs = require('fs');
const paht = require('path');
console.log(paht.join(__dirname,'../file.json'));
let file = JSON.parse(fs.readFileSync(paht.join(__dirname,'../file.json'), "utf8"));
__dirname gives you the directory of your current file, I used path.join to make sure I can go further.
I put the json file in upper directory in my case
Related
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!
I build a Node.js tool in order to change GCP config quickly. But I'm a bit stuck on the parsing of the config_nameOfConfig file.
It looks like this :
[core]
account = email#example.fr
project = project-example
disable_usage_reporting = False
[compute]
region = europe-west1-b
(This file is available in '/Users/nameOfUser/.config/gcloud')
And I want to convert it in an object like this :
const config = {
account:"email#example.fr",
project:"project-example",
disable_usage_reporting:false,
region:"europe-west1-b",
};
I get the content of this file with the fs.readFileSync function who converts it into a string.
An idea ?
This seems to be an ini file format. Unless you want to do the parsing yourself, you'd better just download a library to your project, for example:
npm install ini
Then in the code:
var ini = require('ini')
var fs = require('fs')
var config = ini.parse(fs.readFileSync('./config_nameOfConfig', 'utf-8'))
In config you should now have object containing the data in the file.
You can console.log(config) to see how it exactly looks like.
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
I have created a zip file of a .txt file using node.js zlib and while trying to unzip the file i used the same node_module zlib but its throwing error.Please check my code snippet and please feel free to edit code.I also checked the file location its same as the
Error :
Error: unexpected end of file
at Unzip.zlibOnError (zlib.js:153:15)
Code :
const fs=require('fs');
const zlib = require('zlib');
const iii = fs.createReadStream('test.txt.gz');
const oo = fs.createWriteStream('test1.txt');
const unzip = zlib.createUnzip()
iii.pipe(unzip).pipe(oo)
I am unable to understand my mistake.Can anyone help me with the same?
Thanks in advance!!
Used below code to zip the file.
code to zip:
const fs=require('fs');
const zlib = require('zlib');
const gzip = zlib.createGzip();
const inp = fs.createReadStream('test.txt');
const out = fs.createWriteStream('test.txt.gz');
inp.pipe(gzip).pipe(out);
I was getting an error because the file got corrupted while trying the zip-unzip.
There was no error in the code.
So after deleting and recreating zip file, i was able to unzip it.
Note:
Whenever you try something like this,if you get such error, then always check file size it should not be zero. If it is zero,then recreate the zip and try again.
I am trying to write data to a text file in Node.js. When initializing the write stream I get the following error message:
Be sure you have included at the top of your node program
var fs = require('fs');
Also It looks like you spelled 'Stream' incorrectly
fs.createWriteSteam > fs.createWriteStream
Another issue that may come up is trying to get createWriteStream from 'fs/promises'.
It doesn't exist on 'fs/promises'. (At least on NodeJS version 14);
My solution was to import both:
var fs = require('fs');
var fsPromises = require('fs/promises');