I want data from the config.json file import to the index.js file in the same dir.
so I had it before
JavaScript
x
4
1
const {
2
prefix, token
3
} = require('./config.json');
4
now i would change the string so i need a json object, but how?
Advertisement
Answer
-
In ES5 you can do it by
JavaScript
1
2
1
const object = require('./config.json');
2
The object will contain your JSON.
In ES6
JavaScript
1
2
1
import object from './config.json'
2
-
Using fs module synchronously
JavaScript
1
3
1
const fs = require('fs')
2
const jsonData = JSON.parse(fs.readFileSync('config.json', 'utf-8'))
3