当前位置:网站首页>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永久设置
在系统环境变量下面手动添加即可。
边栏推荐
- Don't ask me again why MySQL hasn't left the index? For these reasons, I'll tell you all
- Is JPMorgan futures safe to open an account? What is the account opening method of JPMorgan futures company?
- Zhang Chi Consulting: lead lithium battery into six sigma consulting to reduce battery capacity attenuation
- 她就是那个「别人家的HR」|ONES 人物
- 工厂高精度定位管理系统,数字化安全生产管理
- Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
- 2022-07-01日报:谷歌新研究:Minerva,用语言模型解决定量推理问题
- ABAP-屏幕切换时,刷新上一个屏幕
- Survey of intrusion detection systems:techniques, datasets and challenges
- 微信小程序01-底部导航栏设置
猜你喜欢
【Pygame实战】你说神奇不神奇?吃豆人+切水果结合出一款你没玩过的新游戏!(附源码)
Deep operator overloading (2)
【300+精选大厂面试题持续分享】大数据运维尖刀面试题专栏(三)
[target tracking] | template update time context information (updatenet) "learning the model update for Siamese trackers"
Returning to the top of the list, the ID is still weak
Zero copy technology of MySQL
[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)
Short Wei Lai grizzly, to "touch China" in the concept of stocks for a living?
Stm32f4-tft-spi timing logic analyzer commissioning record
6.2 normalization 6.2.6 BC normal form (BCNF) 6.2.9 normalization summary
随机推荐
MySQL的零拷贝技术
张驰咨询:锂电池导入六西格玛咨询降低电池容量衰减
Automatique, intelligent, visuel! Forte conviction des huit conceptions derrière la solution sslo
A unifying review of deep and shallow anomaly detection
Wechat applet 03 - text is displayed from left to right, and the block elements in the line are centered
[one day learning awk] conditions and cycles
做空蔚来的灰熊,以“碰瓷”中概股为生?
她就是那个「别人家的HR」|ONES 人物
Phpcms background upload picture button cannot be clicked
Stm32f4-tft-spi timing logic analyzer commissioning record
精益六西格玛项目辅导咨询:集中辅导和点对点辅导两种方式
摩根大通期货开户安全吗?摩根大通期货公司开户方法是什么?
Go语学习笔记 - gorm使用 - 表增删改查 | Web框架Gin(八)
Advanced cross platform application development (24): uni app realizes file download and saving
What time do you get off work?!!!
RT-Thread Env 工具介绍(学习笔记)
Redis seckill demo
STM32ADC模拟/数字转换详解
[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)
你TM到底几点下班?!!!