search

Home  >  Q&A  >  body text

node.js - What exactly is node's env? What is the use?

What does env of node mean as an environment variable? I’ve seen it in many places, but what’s the use?

Why didn’t I learn PHP with the concept of env before?

怪我咯怪我咯2751 days ago687

reply all(4)I'll reply

  • 黄舟

    黄舟2017-05-16 13:33:01

    Environment variables can be set through NODE_ENV (the default value is development). Run the following command in the terminal to get the debugging data of the development environment, such as logs, mongodb data operations, etc. Generally, we perform different processing on the development environment and production environment by checking this value. This value can be set on the command line in the following way:

    linux & mac: export NODE_ENV=production
    windows:set NODE_ENV=production
    For example, if the code needs to do some processing in the production environment, you can write like this:

    if (process.env.NODE_ENV === 'production') {
    // just for production code
    }

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-16 13:33:01

    php also has getenv putenv

    reply
    0
  • 迷茫

    迷茫2017-05-16 13:33:01

    NODE_ENV is the environment variable of node.js and is part of the node.js module search. If it is not set, node.js will report a not found error after installing the global module

    reply
    0
  • ringa_lee

    ringa_lee2017-05-16 13:33:01

    For example, your code has a development (development) environment, a test (test) environment and an online (production-) environment. When you write code, you may develop it locally in the development environment first, then move to the test environment, and finally go online. How to control switching between different environments? This env is used at this time. According to the terminal's export NODE_ENV=test/NODE_ENV=development/NODE_ENV=production, you can get different environment variable parameters. Make different configurations based on these parameters, and then package them to get what you want. Production Environment.

    reply
    0
  • Cancelreply