当前位置:网站首页>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 :
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
:
2.1.2 Permanent settings
Add it manually under the system environment variable .
边栏推荐
- Comment win11 définit - il les permissions de l'utilisateur? Win11 comment définir les permissions de l'utilisateur
- 自動、智能、可視!深信服SSLO方案背後的八大設計
- Nuxt.js数据预取
- RT-Thread Env 工具介绍(学习笔记)
- 基于PHP的轻量企业销售管理系统
- 超视频时代,什么样的技术会成为底座?
- Thinkphp内核工单系统源码商业开源版 多用户+多客服+短信+邮件通知
- 将ABAP On-Premises系统连接到中央检查系统以进行自定义代码迁移
- 关于用 ABAP 代码手动触发 SAP CRM organization Model 自动决定的研究
- 微服务追踪SQL(支持Isto管控下的gorm查询追踪)
猜你喜欢
[target tracking] |stark
Tanabata confession introduction: teach you to use your own profession to say love words, the success rate is 100%, I can only help you here ~ (programmer Series)
Factory high-precision positioning management system, digital safety production management
AVL 平衡二叉搜索树
[stm32-usb-msc problem help] stm32f411ceu6 (Weact) +w25q64+usb-msc flash uses SPI2 to read out only 520kb
ATSs: automatically select samples to eliminate the difference between anchor based and anchor free object detection methods
基于PHP的轻量企业销售管理系统
DO280管理应用部署--pod调度控制
Stm32f4-tft-spi timing logic analyzer commissioning record
[video memory optimization] deep learning video memory optimization method
随机推荐
[target tracking] | template update time context information (updatenet) "learning the model update for Siamese trackers"
Trace the source of drugs and tamp the safety dike
Day 3 of rhcsa study
TensorFlow團隊:我們沒被拋弃
Summer Challenge harmonyos canvas realize clock
Microservice tracking SQL (support Gorm query tracking under isto control)
ThinkPHP进阶
Pnas: brain and behavior changes of social anxiety patients with empathic embarrassment
Crypto Daily:孙宇晨在MC12上倡议用数字化技术解决全球问题
Photoshop plug-in HDR (II) - script development PS plug-in
Connect the ABAP on premises system to the central inspection system for custom code migration
有些能力,是工作中学不来的,看看这篇超过90%同行
Reading notes of top performance version 2 (V) -- file system monitoring
嵌入式开发:5个修订控制最佳实践
[one day learning awk] function and user-defined function
Automatic, intelligent and visual! Deeply convinced of the eight designs behind sslo scheme
TensorFlow团队:我们没被抛弃
Redis seckill demo
电脑屏幕变色了怎么调回来,电脑屏幕颜色怎么改
Raytheon technology rushes to the Beijing stock exchange and plans to raise 540million yuan