Using spread operator for process.env

I faced strange behavior when I used spread operator for process.env. In my project, our main source is Typescript but we call some API written in C and C++. Since it is not possible to call the API directly from Typescript world we created a wrapper for it written in C++. The C++ is written with nan module and nan will generate xxx.node file. Our application can load both xxx.node file and DLL1 but DLL1 cannot find DLL2 because added path to process.env.PATH is somehow not applied to C++ world.

We used spread operator for process.env in order to add several values but it breaks internal hierarchy. All objects have __proto__ property which has __proto__ property which has __proto__ which has …… but the hierarchy was somehow broken by using spread operator. Our code looked like this.

// app.ts
process.env = {
    hage: "value1",
    hige: "value2",
    hoge: "value3",

  // index.ts in other module
  if(isFound) {
    process.env.PATH = `${path.join(__dirname, "path1")};${process.env.PATH}`;
  } else {
    process.env.PATH = `${path.join(__dirname, "path2")};${process.env.PATH}`;

I haven’t found the source code to pass the values, which are added to process.env.PATH in Typescript world, to C++ world but when I changed this to following code, DLL1 could load DLL2 correctly.

// app.ts
process.env.hage = "value1";
process.env.hige = "value2";
process.env.hoge = "value3";

I found many functions requires “env” argument as pointer in V8 engine. When it is called in Node.js it is I guess Object which has __proto__ property. The functions defined in C++ world try to call one of functions which is under __proto__ but it doesn’t exist because of hierarchy change and added values were not referred by DLL1.


Copied title and URL