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

146
Visualizações
Is it possible to set system environment variables from a node script?

I have an npm script like this that executes two node files:

package.json

{
  "scripts": {
    "my-target": "node setup.js && node run.js"
  }
}

Ideally, I can set environment variables in setup.js that can then be accessed in run.js.

setup.js

process.env.HELLO_WORLD=1

run.js

// Ideally prints `1`, instead prints undefined.
console.log(process.env.HELLO_WORLD);

Is something like this possible? Ideally the result of setting the environment variables in setup.js would be persistent, so that if I run

$ echo $HELLO_WORLD

from bash, that reference would still be there.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can set env vars when you run the main program:

HELLO_WORLD=hello npm run my-target

package.json:

  "scripts": {
    "my-target": "node setup.js && node run.js"
  },

setup.js:

console.info('setup', process.env.HELLO_WORLD);

run.js:

console.info('run', process.env.HELLO_WORLD);

Output will be

setup hello
run hello
about 4 years ago · Juan Pablo Isaza 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