0

I'm trying to set NODE_EXTRA_CA_CERTS in a node script like so:

// package.json
"scripts": {
    "load-cert": "export NODE_EXTRA_CA_CERTS=cert.pem",
    "start": "node main.js"
  },

npm run load-cert && npm run start

But that doesn't work (gives SSL error). Specifically, I think, because I'm loading the contract in its own separate script then running a different script/command.

This also doesn't work

"scripts": {
    "load-cert": "export NODE_EXTRA_CA_CERTS=riotgames.pem",
    "start": "npm run load-cert && node main.js"
  },

npm run start

The following script works:

"scripts": {
    "start": "export NODE_EXTRA_CA_CERTS=cert.pem && node main.js"
  },

npm run start

Weirdly enough, this also works:

"scripts": {
    "start": "node main.js",
    "load-cert-start": "export NODE_EXTRA_CA_CERTS=riotgames.pem && npm run start"
  },

npm run load-cert-start

Can anyone help me understand why this is happening and if there's a way I can have a script to just load the certificate?

2
  • 1
    It's rather about bash than npm or node Commented Sep 29, 2022 at 20:49
  • I appreciate you adding the tag :) Commented Sep 29, 2022 at 21:17

1 Answer 1

2

Setting an environment variable with export sets it for the current process and all child processes, but NOT the parent process. When you run npm run load-cert, that is a new child process of your shell. When you run npm run start, that is a different child process of your shell and is not a child process of the shell that exported the variable.

* Your shell
  \ * npm run load-cert {}
    \ * shell {NODE_EXTRA_CA_CERTS=riotgames.pem}
  \ * npm run load-cert {}

If you combine the two commands together, as in export NODE_EXTRA_CA_CERTS=cert.pem && node main.js, then the node process is a child process of the shell with the environment variable set:

* Your shell
  \ * npm run load-cert {}
    \ * shell {NODE_EXTRA_CA_CERTS=riotgames.pem}
      \ * node {NODE_EXTRA_CA_CERTS=riotgames.pem}

Or export NODE_EXTRA_CA_CERTS=riotgames.pem && npm run start

* Your shell
  \ * npm run load-cert {}
    \ * shell {NODE_EXTRA_CA_CERTS=riotgames.pem}
      \ * npm run start {NODE_EXTRA_CA_CERTS=riotgames.pem}
        \ * node {NODE_EXTRA_CA_CERTS=riotgames.pem}

And for npm run load-cert && node main.js

* Your shell
  \ * npm run start {}
    \ * shell started by npm
      \ * npm run load-cert {}
        \ * shell {NODE_EXTRA_CA_CERTS=riotgames.pem}
      \ * node {} <-- Sibling, not child, of npm run load-cert

(Note I omitted a couple of non-relevant processes in-between in these diagrams)

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

8 Comments

Thank you so much! If I understand you fully, run is the main culprit, spawning new processes with npm run load-cert && npm run start. But what I don't get, is why the second non-working example is spawning two processes, while the second working example is spawning child processes, even though they both run once.
@Brother58697, Added that case to the examples.
In the last example, shouldn't node {} be on the same level of shell {NODE_EXTRA_CA_CERTS=riotgames.pem}, as in they're siblings? Since node runs after load-cert so it becomes its child, but load-cert exports the variable in another child process.
No. npm run load-cert does not start node. Both are started by a shell started by npm run start (previously not shown in the diagram). The && is implemented by the shell. Also, running after does not mean it "becomes" its child.
Generally speaking, every process has an environment block in the kernel. This block stores all environment variables. export just modifies the values in this block. It is not a process, but a shell built-in command (handled by the shell itself). Whenever a new process is created, it receives a copy of the environment block of the process that created it. Obviously then, changes to the copy do not affect the original. A process uses the same environment block for its entire lifetime.
|

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.