当前位置:网站首页>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 .
边栏推荐
- vim 从嫌弃到依赖(22)——自动补全
- Pocket Network为Moonbeam和Moonriver RPC层提供支持
- 如何写出好代码 - 防御式编程指南
- Don't ask me again why MySQL hasn't left the index? For these reasons, I'll tell you all
- Introduction to RT thread env tool (learning notes)
- What time do you get off work?!!!
- Crypto Daily:孙宇晨在MC12上倡议用数字化技术解决全球问题
- Thinkphp内核工单系统源码商业开源版 多用户+多客服+短信+邮件通知
- Redis秒杀demo
- 超视频时代,什么样的技术会成为底座?
猜你喜欢

Go语学习笔记 - gorm使用 - 表增删改查 | Web框架Gin(八)

【LeetCode】43. 字符串相乘

精益六西格玛项目辅导咨询:集中辅导和点对点辅导两种方式

process.env.NODE_ENV

Lean Six Sigma project counseling: centralized counseling and point-to-point counseling

华为发布HCSP-Solution-5G Security人才认证,助力5G安全人才生态建设

Raytheon technology rushes to the Beijing stock exchange and plans to raise 540million yuan

MySQL advanced 4
![[stm32-usb-msc problem help] stm32f411ceu6 (Weact) +w25q64+usb-msc flash uses SPI2 to read out only 520kb](/img/ec/fa51b21468708609f998de1b2b84fe.jpg)
[stm32-usb-msc problem help] stm32f411ceu6 (Weact) +w25q64+usb-msc flash uses SPI2 to read out only 520kb

Introduction to RT thread env tool (learning notes)
随机推荐
6.2 normalization 6.2.6 BC normal form (BCNF) 6.2.9 normalization summary
Stm32f4-tft-spi timing logic analyzer commissioning record
Smart Party Building: faith through time and space | 7.1 dedication
Pocket Network为Moonbeam和Moonriver RPC层提供支持
张驰咨询:家电企业用六西格玛项目减少客户非合理退货案例
【开源数据】基于虚拟现实场景的跨模态(磁共振、脑磁图、眼动)人类空间记忆研究开源数据集
药品溯源夯实安全大堤
Thinkphp内核工单系统源码商业开源版 多用户+多客服+短信+邮件通知
Deep operator overloading (2)
[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (III)
ADS算力芯片的多模型架构研究
AVL 平衡二叉搜索树
【STM32学习】 基于STM32 USB存储设备的w25qxx自动判断容量检测
Introduction to RT thread env tool (learning notes)
远程办公经验?来一场自问自答的介绍吧~ | 社区征文
[STM32 learning] w25qxx automatic judgment capacity detection based on STM32 USB storage device
process.env.NODE_ENV
搜索框和按钮缩放时会有缝隙的bug
嵌入式开发:5个修订控制最佳实践
Crypto Daily:孙宇晨在MC12上倡议用数字化技术解决全球问题