当前位置:网站首页>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 .
边栏推荐
- 2023届春招实习-个人面试过程和面经分享
- RT-Thread Env 工具介绍(学习笔记)
- 2022 Moonriver全球黑客松优胜项目名单
- Pocket Network为Moonbeam和Moonriver RPC层提供支持
- 【IDM】IDM下载器安装
- Smart Party Building: faith through time and space | 7.1 dedication
- Microservice tracking SQL (support Gorm query tracking under isto control)
- 微服务追踪SQL(支持Isto管控下的gorm查询追踪)
- One revolution, two forces, three links: the "carbon reduction" roadmap behind the industrial energy efficiency improvement action plan
- Équipe tensflow: Nous ne sommes pas abandonnés
猜你喜欢

DO280管理应用部署--pod调度控制

Équipe tensflow: Nous ne sommes pas abandonnés
![Stm32f411 SPI2 output error, pb15 has no pulse debugging record [finally, pb15 and pb14 were found to be short circuited]](/img/ea/8c9f716717bc08f2e563c577738ec8.png)
Stm32f411 SPI2 output error, pb15 has no pulse debugging record [finally, pb15 and pb14 were found to be short circuited]

从 MLPerf 谈起:如何引领 AI 加速器的下一波浪潮

Zhang Chi Consulting: household appliance enterprises use Six Sigma projects to reduce customers' unreasonable return cases

周少剑,很少见

Go language learning notes - Gorm use - table addition, deletion, modification and query | web framework gin (VIII)

嵌入式开发:5个修订控制最佳实践

Thinkphp内核工单系统源码商业开源版 多用户+多客服+短信+邮件通知

Lean Six Sigma project counseling: centralized counseling and point-to-point counseling
随机推荐
超视频时代,什么样的技术会成为底座?
[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (III)
[200 opencv routines] 216 Draw polylines and polygons
MySQL高级篇4
工厂高精度定位管理系统,数字化安全生产管理
一次革命、两股力量、三大环节:《工业能效提升行动计划》背后的“减碳”路线图...
TensorFlow團隊:我們沒被拋弃
电脑照片尺寸如何调整成自己想要的
Seate中用了shardingjdbc 就不能用全局事务了吗?
周少剑,很少见
【STM32学习】 基于STM32 USB存储设备的w25qxx自动判断容量检测
智慧党建: 穿越时空的信仰 | 7·1 献礼
Deep operator overloading (2)
SAP CRM organization Model(组织架构模型)自动决定的逻辑分析
【STM32-USB-MSC问题求助】STM32F411CEU6 (WeAct)+w25q64+USB-MSC Flash用SPI2 读出容量只有520KB
The newly born robot dog can walk by himself after rolling for an hour. The latest achievement of Wu Enda's eldest disciple
For the sustainable development of software testing, we must learn to knock code?
MySQL advanced 4
Raytheon technology rushes to the Beijing stock exchange and plans to raise 540million yuan
使用 csv 导入的方式在 SAP S/4HANA 里创建 employee 数据