0

I need to set some environment variables from within a script.

Those variables must:

  1. be visible to other processes in the same shell (not just child processes created by this node process)
  2. outlive the node process that set the variables

It is not good enough to manipulate process.env. I've seen a couple of SO questions that sound identical to mine, but both of them accepted answers to the effect of "assign values to process.env[your_var]", which fails both of my criteria.

I was using a little sugar on top of execa, to do (basically) this:

await execa( `export MY_VAR=osmethne` )

But that blows up:

Error: Command failed with exit code 2 (ENOENT): export MY_VAR=osmethne
spawn export ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:240:19)
    at onErrorNT (internal/child_process.js:415:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)
error Command failed with exit code 1.

This all happens in Dockerized linux. So, I don't need to support Windows or OS/2 Warp or any other nonsense.

3
  • I personally would need more to go on... like a miminal test case "outlive the node process that set the variables" is to cryptic for my taste. What is the use case? But ... maybe there are some NodeJs gurus out there who click with this description. Upvoting.... Commented Aug 2, 2019 at 19:15
  • It looks like this may be a duplicate of question. According to that it's not possible to do exactly what you want. Perhaps if you describe the overall problem you are trying to solve we can offer alternative solutions. Commented Aug 2, 2019 at 21:06
  • 1
    If this was possible it'd be a potential security risk as well. Commented Aug 3, 2019 at 0:52

1 Answer 1

-1

Try

require('dotenv').config()

in your code

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.