当前位置:网站首页>nodejs--process
nodejs--process
2022-08-01 23:21:00 【lazytomato】
start
- 前几天阅读了一下公司脚手架的源码。
- 在整体的阅读中,遇到一个很熟悉又很陌生的一个单词
process - 今天来了解一下这个单词。
出现的场景
- 记录一下我遇到这个单词的使用场景
- 在脚手架的代码中是这样使用的 :
process.cwd()
官方文档解释

The process.cwd() method returns the current working directory of the Node.js process.
英译process.cwd()方法返回Node.js进程的当前工作目录
process
英译:进程,步骤,程序
比较好理解,就是返回当前进程所在的工作目录。
说个题外话,看到
cwd我联想到了之前学习的linux命令pwd,看一下两者的区别
whatis pwd print name of current/working directory 英译: 打印当前/工作目录的名称 (意思差不多,这里的 p 是 print 的缩写)
认识
process 英译是进程的意思。是一个对象,该process对象提供有关和控制当前 Node.js 进程的信息。
它是node环境下的全局变量。
既然想了解它,走,打印一下它。
// 1.js
console.log(process)
node 1.js --ceshi --tomato
输出截图

属性太多了,就不全部展示出来了,说几个常见的
process.cwd()返回Node.js进程的当前工作目录
process.argv该
process.argv属性返回一个数组,其中包含启动 Node.js 进程时传递的命令行参数。process.env该
process.env属性返回一个包含用户环境的对象。process.platform获取当前进程运行的操作系统平台
process.nextTick()process.nextTick() adds callback to the "next tick queue". This queue is fully drained after the current operation on the JavaScript stack runs to completion and before the event loop is allowed to continue. process.nextTick() 添加 一个回调函数 到 下一个任务队列 在JavaScript堆栈上的当前操作运行到完成,并且允许事件循环继续之前,该队列将被完全耗尽。
解释
process.cwd()不用说,刚解释过了process.argv可以用来获取 我们输入执行文件的同时 传入的参数例如:
执行
node 1.js --ceshi --tomato输出:
argv: [ 'E:\\nodejs\\node.exe', 'C:\\Users\\17607\\OneDrive\\MD笔记\\myNote\\process\\1.js', '--ceshi', '--tomato' ],process.env可以在这变量上绑定一些属性例如: 最常见的绑定变量 NODE_ENV,用来区分开发环境。
// 1.js process.env.NODE_ENV = development // process.env.NODE_ENV = productionprocess.platform例如:
// 1.js console.log(process.platform) # node 1.js win32process.nextTick()process.nextTick中的回调是在当前tick执行完之后,下一个宏任务执行之前调用的。简单理解,
process.nextTick微任务,但是它领先于 promise的微任务。var flag = false process.nextTick(() => { console.log(1) }) Promise.resolve().then(() => { console.log('then1') flag = true }) new Promise((resolve) => { console.log('promise') resolve() setTimeout(() => { console.log('timeout2') }, 10) }).then(function () { console.log('then2') }) function f1(f) { f() } function f2(f) { setTimeout(f) } f1(() => console.log('f为:', flag ? '异步' : '同步')) f2(() => { console.log('timeout1,', 'f为:', flag ? '异步' : '同步') }) process.nextTick(() => { console.log(2) }) console.log('本轮宏任务执行完') // 重点注意一下 process.nextTick的打印 ,它领先于promsie的微任务。它在 是在当前宏任务执行完 执行。 // 其他的就是事件循环的问题了,本文暂不展开。
end
总结一下
- process是一个全局对象。
- process中包含对进程的一些信息。
- 常用的 就例如 获取环境变量;获取参数;获取执行路径
边栏推荐
- When using DocumentFragments add a large number of elements
- sys_kill系统调用
- 用virtualenv和Virtualenvwrapper虚拟环境管理工具创建虚拟环境
- excel split text into different rows
- 【参营经历贴】2022网安夏令营
- 还在纠结报表工具的选型么?来看看这个
- TCP 可靠吗?为什么?
- 深度学习基础-基于Numpy的循环神经网络(RNN)实现和反向传播训练
- npm包【详解】(内含npm包的开发、发布、安装、更新、搜索、卸载、查看、版本号更新规则、package.json详解等)
- 检查点是否在矩形内
猜你喜欢

From 0 to 1: Design and R&D Notes of Graphic Voting Mini Program

牛客多校4 A.Task Computing 思维

chrome copies the base64 data of an image

论文理解【RL - Exp Replay】—— Experience Replay with Likelihood-free Importance Weights

xss相关知识点以及从 XSS Payload 学习浏览器解码
![Thesis understanding [RL - Exp Replay] - Experience Replay with Likelihood-free Importance Weights](/img/f1/9824f32dd4fe4b3e94af3f945b1801.png)
Thesis understanding [RL - Exp Replay] - Experience Replay with Likelihood-free Importance Weights

y84.第四章 Prometheus大厂监控体系及实战 -- prometheus告警机制进阶(十五)

系统可用性:SRE口中的3个9,4个9...到底是个什么东西?

程序员如何优雅地解决线上问题?
![[Camp Experience Post] 2022 Cybersecurity Summer Camp](/img/1e/716bafc679dc67d3d54bcc21a3b670.png)
[Camp Experience Post] 2022 Cybersecurity Summer Camp
随机推荐
计算两点之间的距离
npm npm
数据库表设计规则
Department project source code sharing
仿牛客网项目第三章:开发社区核心功能(详细步骤和思路)
excel vertical to horizontal
Oracle 数据库设置为只读及读写
cmd command
Chapter 11 Working with Dates and Times
Quarantine and downgrade
Making a Simple 3D Renderer
Calculate the distance between two points
What is CICD excuse me
问题解决方式了
【数据分析03】
C#大型互联网平台管理框架源码:基于ASP.NET MVC+EF6+Bootstrap开发,支持多数据库
Is TCP reliable?Why?
6132. All the elements in the array is equal to zero - quick sort method
美赞臣EDI 940仓库装运订单详解
SRv6 L3VPN的工作原理
