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

2.2K
Vistas
how to import constants from another file in node.js

Node.js v16.1.0

I am trying to import constants from another js file in the same directory. But i am not being able to do so.

constants.js

export const API_URL = 'http://localhost:8080';
export const MAX_THREADS = 8;

solution.js

/**
 * Import constants from the constants.js file
 * 
 * Each console.log should log a separate constant
 */

import { MAX_THREADS } from "./constants.js";
over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You should do something like the following.

constants.js

const API_URL = 'http://url.com'
const MAX_THREADS = 5

exports.API_URL = API_URL
exports.MAX_THREADS = MAX_THREADS

solution.js

const { API_URL, MAX_THREADS } = require('./constants');
over 4 years ago · Santiago Trujillo Denunciar

0

Have you enabled ES modules? Read about it here: https://nodejs.org/api/esm.html#esm_enabling

over 4 years ago · Santiago Trujillo Denunciar

0

Node.js >= v13

You only need to follow below step be able to use ECMAScript modules.

Add "type": "module" in your package.json.

{
  ...
  "type": "module"
}
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