简体   繁体   中英

Where are "process.env" properties defined?

I am reading a microservice NodeJS project that has some files like below:

index.ts:

import mongoose from "mongoose";
import { app } from "./app";
import { natsWrapper } from "./nats-wrapper";
import { OrderCreatedListener } from "./events/listeners/order-created-listener";
import { OrderCancelledListener } from "./events/listeners/order-cancelled-listener";

const start = async () => {
  if (!process.env.JWT_KEY) {
    throw new Error("JWT_KEY must be defined");
  }
  if (!process.env.MONGO_URI) {
    throw new Error("MONGO_URI must be defined");
  }
  if (!process.env.NATS_CLIENT_ID) {
    throw new Error("NATS_CLIENT_ID must be defined");
  }
  if (!process.env.NATS_URL) {
    throw new Error("NATS_URL must be defined");
  }
  if (!process.env.NATS_CLUSTER_ID) {
    throw new Error("NATS_CLUSTER_ID must be defined");
  }

  try {
    await natsWrapper.connect(
      process.env.NATS_CLUSTER_ID,
      process.env.NATS_CLIENT_ID,
      process.env.NATS_URL
    );
    natsWrapper.client.on("close", () => {
      console.log("NATS connection closed!");
      process.exit();
    });
    process.on("SIGINT", () => natsWrapper.client.close());
    process.on("SIGTERM", () => natsWrapper.client.close());

    new OrderCreatedListener(natsWrapper.client).listen();
    new OrderCancelledListener(natsWrapper.client).listen();

    await mongoose.connect(process.env.MONGO_URI, {});
    console.log("Connected to MongoDb");
  } catch (err) {
    console.error(err);
  }

  app.listen(3000, () => {
    console.log("Listening on port 3000!!!!!!!!");
  });
};

start();

i searched all through the project to find any intialization for process.env.NATS_CLUSTER_ID, process.env.NATS_URL, process.env.MONGO_URI variables/properties, but I couldn't find anything. I only could find process.env.JWT_KEY = "some-secret-key"; .

I am wondering to know where do process.env properties get initialize and when the initialization happens and where do they store the values?

Where are "process.env" properties defined?

process.env refers to an object that consists of name/values pairs that represent the current state of the environment variables within your nodejs process.

That object is initialized from the OS process environment that is present when the nodejs process is first started and that will be either the environment in the command shell you launched nodejs from or the environment that was passed into it when nodejs was launched programmatically. That initialization happens by calling a libuv function called uv_os_getenv() which you can see in the libuv source here for Unix or here for Windows. That data is then stored within nodejs as an object and can then be modified during the running of your nodejs process.

I am wondering to know where do process.env properties get initialize and when the initialization happens

This happens as part of the nodejs process initialization before it starts running your Javascript code.

where do they store the values

process.env is a normal Javascript property that contains a normal Javascript object that contains what nodejs read in initially from the os environment given to your nodejs process plus any environment variables that nodejs itself might set.

You can do console.log(process.env) and see such an object. I initially thought that maybe it would be a getter to get you a copy of the master object, but that's not the case. It is just an ordinary property that contains the actual object and it can be modified as such using Javascript during the running of your program.


In your specific nats programming example, it appears that the environment variables: NATS_CLUSTER_ID , NATS_CLIENT_ID and NATS_URL are either set programmatically by your nats-wrapper module or that module expects you to set these in the environment so they can be retrieved and used from there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM