当前位置:网站首页>process.env.NODE_ENV
process.env.NODE_ENV
2022-07-01 15:43:00 【黑猫几绛】
一、process.env
查阅文档的时候可以看到这样一句话:process 对象提供有关当前 Node.js 进程的信息并对其进行控制。
const process = require('node:process');
在计组的知识点中我们知道:process(进程)是系统进行资源分配和调度的基本单位,是操作系统结构的基础。那么,在node.js中,process是否也有操作系统结构的信息呢?
打印一下看看:
const process = require('process')
const express = require('express')
const app = express()
app.get('/',(req,res)=>{
console.log(process);
})
app.listen(port, host)
访问localhost:3000后查看控制台:

可以看到,process代表的值是计算机系统相关的信息,而process.env则表示的是系统环境变量。
理解:process可以看作是node.js中的一个全局变量。
二、NODE_ENV
注意:我本来想在文档中找找这个属性的,可是它并不存在于文档中,这是因为,NODE_ENV仅仅是一个自定义变量,据说最早是express社区中流传这个自定义变量,后来慢慢就成为了前端开发中的一个使用规范。
注意看上面那一张图片中框起来的部分,为什么会加上这个环境变量呢?
这是因为:在开发的过程中,我们可能需要同时面临多种环境。比如:
- 开发:API_URL =Ihttp://127.0.0.1:3000
- 线上部署环境:API_URL =https://imNeko:3000
这也是为什么,有时候我们在开发的时候感觉代码项目没有问题,可是部署到线上时出现了差错。
在这里我以一个基于Nuxt.js的项目中的package.json来举例:
"scripts": {
// 让 cross-env 把 NODE_ENV 设置为 development, 再运行指定文件 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"
},
从上面的代码可以看出:
当我们执行npm run dev的时候,NODE_ENV 将会被注入到 process.env 对象上,并且值为 development,我们 只 可以在 server/index.js 脚本中以及它所引入的脚本中访问到 process.env.NODE_ENV,而无法在其它脚本中访问
不过,如果采用的是webpack打包方式,注册下面这个插件既可以让其他文件访问到process.env.NODE_ENV
const webpack = require('webpack');
module.exports = {
// ...
plugins: [
// DefinePlugin允许我们创建全局变量,可以在编译时进行设置
new webpack.DefinePlugin({
'process.env.NODE_ENV': '"development"'
})
]
// ...
}
现在看看如何在项目中切换环境变量:
let env = process.env.NODE_ENV
let cssSourceMapDev = (env === 'development' && config.dev.cssSourceMap)
let cssSourceMapProduct = (env === 'production' && config.build.productionSourceMap)
let useCssSourceMap = cssSourceMapDev || cssSourceMapProduct
我们可以根据process.env.NODE_ENV的值来选择编译打包什么文件。
2.1 设置NODE_ENV
2.1.1临时设置
在cmd窗口中输入这段代码:
set NODE_ENV = production
// set NODE_ENV = development
然后打印process:

2.1.2永久设置
在系统环境变量下面手动添加即可。
边栏推荐
- 【显存优化】深度学习显存优化方法
- ABAP-调用Restful API
- Task.Run(), Task.Factory.StartNew() 和 New Task() 的行为不一致分析
- Deep operator overloading (2)
- 说明 | 华为云云商店「商品推荐榜」
- 硬件开发笔记(九): 硬件开发基本流程,制作一个USB转RS232的模块(八):创建asm1117-3.3V封装库并关联原理图元器件
- [STM32 learning] w25qxx automatic judgment capacity detection based on STM32 USB storage device
- 2022 Moonriver全球黑客松优胜项目名单
- RT-Thread Env 工具介绍(学习笔记)
- Samsung took the lead in putting 3nm chips into production, and Shanghai's fresh master students can settle directly. Nankai has established a chip science center. Today, more big news is here
猜你喜欢

雷神科技冲刺北交所,拟募集资金5.4亿元

【STM32学习】 基于STM32 USB存储设备的w25qxx自动判断容量检测

你TM到底几点下班?!!!

做空蔚来的灰熊,以“碰瓷”中概股为生?
![[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (III)](/img/cf/44b3983dd5d5f7b92d90d918215908.png)
[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (III)

最新NLP赛事实践总结!

Returning to the top of the list, the ID is still weak

Hardware development notes (9): basic process of hardware development, making a USB to RS232 module (8): create asm1117-3.3v package library and associate principle graphic devices
![[pyGame practice] do you think it's magical? Pac Man + cutting fruit combine to create a new game you haven't played! (source code attached)](/img/0a/c1a4b57b9729e0cf9de1feae9f8c19.png)
[pyGame practice] do you think it's magical? Pac Man + cutting fruit combine to create a new game you haven't played! (source code attached)

GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
随机推荐
Équipe tensflow: Nous ne sommes pas abandonnés
[one day learning awk] function and user-defined function
[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (III)
Sort out the four commonly used sorting functions in SQL
The last picture is seamlessly connected with the first picture in the swiper rotation picture
【300+精选大厂面试题持续分享】大数据运维尖刀面试题专栏(三)
Trace the source of drugs and tamp the safety dike
【显存优化】深度学习显存优化方法
STM32ADC模拟/数字转换详解
Pocket Network为Moonbeam和Moonriver RPC层提供支持
Deep operator overloading (2)
[video memory optimization] deep learning video memory optimization method
Zhang Chi Consulting: lead lithium battery into six sigma consulting to reduce battery capacity attenuation
Qt+pcl Chapter 9 point cloud reconstruction Series 2
Detailed explanation of stm32adc analog / digital conversion
Lean Six Sigma project counseling: centralized counseling and point-to-point counseling
One revolution, two forces, three links: the "carbon reduction" roadmap behind the industrial energy efficiency improvement action plan
C#/VB.NET 合并PDF文档
Automatic, intelligent and visual! Deeply convinced of the eight designs behind sslo scheme
软件测试的可持续发展,必须要学会敲代码?