Skip to content

import JSON file with node.js

I want data from the config.json file import to the index.js file in the same dir.

so I had it before

const {
prefix, token
} = require('./config.json');

now i would change the string so i need a json object, but how?

Answer

  1. In ES5 you can do it by

const object = require('./config.json');

The object will contain your JSON.

In ES6

import object from './config.json'
  1. Using fs module synchronously

const fs = require('fs')
const jsonData = JSON.parse(fs.readFileSync('config.json', 'utf-8'))