Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

2.2K
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

0

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

over 4 years ago · Santiago Trujillo Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda