Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

368
Vistas
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?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

  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'))
over 4 years ago · Santiago Trujillo Denunciar

0

Async example, if you're using ES6 modules:

import fs from 'fs/promises';

const {prefix, token} = JSON.parse(
  await fs.readFile('./config.json', 'utf-8')
);

See also:

  • https://nodejs.org/api/fs.html#promises-api
  • https://nodejs.org/api/packages.html#determining-module-system
over 4 years ago · Santiago Trujillo Denunciar

0

If you want the whole object, simply use:

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

Otherwise if you want the contents of config.json, you can simply use the fs module to read the file. More specifically fs.readFileSync if you want it to be synchronous.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda