当前位置:网站首页>process. env. NODE_ ENV

process. env. NODE_ ENV

2022-07-01 15:54:00 Black cat crimson

One 、process.env

When looking up the document, you can see such a sentence :process Object provides information about the current Node.js Process information and control it .

const process = require('node:process');

In the knowledge points of the group, we know :process( process ) Is the system resource allocation and scheduling of the basic unit , Is the foundation of the operating system architecture . that , stay node.js in ,process Is there also information about the structure of the operating system ?

Print it and have a look :

const process = require('process')
const express = require('express')
const app = express()

app.get('/',(req,res)=>{
    
    console.log(process);
})

app.listen(port, host)

visit localhost:3000 View the console after :

 Insert picture description here

You can see ,process The value represented is the information related to the computer system , and process.env It means System environment variable .

understand process It can be seen as node.js A global variable in .

Two 、NODE_ENV

Be careful : I originally wanted to find this attribute in the document , But it doesn't exist in the document , This is because ,NODE_ENV Just a custom variable , It is said that the earliest was express Stream this custom variable in the community , Later, it gradually became a usage specification in front-end development .

Pay attention to the framed part in the picture above , Why add this environment variable ?

This is because : In the process of development , We may need to face multiple environments at the same time . such as :

  • Development :API_URL =Ihttp://127.0.0.1:3000
  • Online deployment environment :API_URL =https://imNeko:3000

That's why , Sometimes when we are developing, we feel that there is no problem with the code project , But there was a mistake when it was deployed online .

Here I base on Nuxt.js In the project of package.json For example :

"scripts": {
    
    //  Give Way  cross-env  hold  NODE_ENV  Set to  development,  Rerun the specified file  server/index.js
    "dev": "cross-env NODE_ENV=development nodemon server/index.js --watch server",
    "build": "nuxt build",
    "start": "cross-env NODE_ENV=production node server/index.js",
    "generate": "nuxt generate"
},

As you can see from the code above :

When we execute npm run dev When ,NODE_ENV Will be injected into process.env On the object , And the value of development, We only Can be in server/index.js In the script and the script it introduces process.env.NODE_ENV, Cannot be accessed in other scripts

however , If you use webpack packaging , Register the following plug-in to allow other files to access process.env.NODE_ENV

const webpack = require('webpack');
module.exports = {
    
  // ...
  plugins: [
    // DefinePlugin Allows us to create global variables , It can be set at compile time 
    new webpack.DefinePlugin({
    
      'process.env.NODE_ENV': '"development"'
    })
  ]
  // ...
}

Now look at how to switch environment variables in a project :

let env = process.env.NODE_ENV
let cssSourceMapDev = (env === 'development' && config.dev.cssSourceMap)
let cssSourceMapProduct = (env === 'production' && config.build.productionSourceMap)
let useCssSourceMap = cssSourceMapDev || cssSourceMapProduct

We can use process.env.NODE_ENV To choose what files to compile and package .

2.1 Set up NODE_ENV

2.1.1 Temporary settings

stay cmd Enter this code in the window :

set NODE_ENV = production
// set NODE_ENV = development

And then print process

 Insert picture description here

2.1.2 Permanent settings

Add it manually under the system environment variable .

原网站

版权声明
本文为[Black cat crimson]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/182/202207011543171515.html