当前位置:网站首页>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 .
边栏推荐
- [video memory optimization] deep learning video memory optimization method
- [200 opencv routines] 216 Draw polylines and polygons
- 摩根大通期货开户安全吗?摩根大通期货公司开户方法是什么?
- SAP s/4hana: one code line, many choices
- 嵌入式开发:5个修订控制最佳实践
- ThinkPHP进阶
- Please, stop painting star! This has nothing to do with patriotism!
- 【LeetCode】43. 字符串相乘
- Overview | slam of laser and vision fusion
- 《性能之巅第2版》阅读笔记(五)--file-system监测
猜你喜欢
Zhang Chi Consulting: lead lithium battery into six sigma consulting to reduce battery capacity attenuation
u本位合约和币本位合约有区别,u本位合约会爆仓吗
Équipe tensflow: Nous ne sommes pas abandonnés
大龄测试/开发程序员该何去何从?是否会被时代抛弃?
七夕表白攻略:教你用自己的专业说情话,成功率100%,我只能帮你们到这里了啊~(程序员系列)
MySQL advanced 4
C#/VB.NET 合并PDF文档
One revolution, two forces, three links: the "carbon reduction" roadmap behind the industrial energy efficiency improvement action plan
Summer Challenge harmonyos canvas realize clock
Please, stop painting star! This has nothing to do with patriotism!
随机推荐
One revolution, two forces, three links: the "carbon reduction" roadmap behind the industrial energy efficiency improvement action plan
Task.Run(), Task.Factory.StartNew() 和 New Task() 的行为不一致分析
SAP s/4hana: one code line, many choices
说明 | 华为云云商店「商品推荐榜」
6.2 normalization 6.2.6 BC normal form (BCNF) 6.2.9 normalization summary
The newly born robot dog can walk by himself after rolling for an hour. The latest achievement of Wu Enda's eldest disciple
SAP CRM organization Model(组织架构模型)自动决定的逻辑分析
AVL balanced binary search tree
MySQL的零拷贝技术
Introduction to RT thread env tool (learning notes)
Research on manually triggering automatic decision of SAP CRM organization model with ABAP code
Go语学习笔记 - gorm使用 - 表增删改查 | Web框架Gin(八)
Create employee data in SAP s/4hana by importing CSV
Redis秒杀demo
ThinkPHP进阶
【300+精选大厂面试题持续分享】大数据运维尖刀面试题专栏(三)
工厂高精度定位管理系统,数字化安全生产管理
你TM到底几点下班?!!!
智慧党建: 穿越时空的信仰 | 7·1 献礼
ABAP-调用Restful API