【发布时间】:2023-03-03 02:21:01
【问题描述】:
在线currentEnvironment = typeof (process.env.NODE_ENV === 'string') ? currentEnvironment.toLowerCase() : '';
当我通过输入 ???? uptime-monitoring-app >> NODE_ENV=production node app.js 时,它可以正常工作。
Command Line arguments :
0: /usr/local/Cellar/node@10/10.15.0/bin/node
1: /Users/tentacion/Documents/CODE/Node without NPM/uptime-monitoring-app/app.js
string
Server Started @ 5000 Environment : production
--------------------------------------------------------------------------------------------------
但是对于输入 uptime-monitoring-app >> Nnode app.js 它会抛出错误,因为现在 NODE_ENV 变量现在是未定义的。
Command Line arguments :
0: /usr/local/Cellar/node@10/10.15.0/bin/node
1: /Users/tentacion/Documents/CODE/Node without NPM/uptime-monitoring-app/app.js
undefined
/Users/tentacion/Documents/CODE/Node without NPM/uptime-monitoring-app/envConfig.js:66
currentEnvironment = typeof (process.env.NODE_ENV === 'string') ? currentEnvironment.toLowerCase() : '';
^
TypeError: Cannot read property 'toLowerCase' of undefined
at Object.<anonymous> (/Users/tentacion/Documents/CODE/Node without NPM/uptime-monitoring-app/envConfig.js:66:86)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (/Users/tentacion/Documents/CODE/Node without NPM/uptime-monitoring-app/app.js:11:19)
at Module._compile (internal/modules/cjs/loader.js:689:30)
???? uptime-monitoring-app >>
我所期望的
如果NODE_ENV 变量未定义,则表达式typeof (process.env.NODE_ENV === 'string') 将为假,因此应执行下一条语句。即用空字符串分配它。
请帮忙,我不知道我做错了什么。我是节点新手
【问题讨论】:
-
typeof (process.env.NODE_ENV === 'string') ?将评估为typeof (true) ?或typeof (false) ?,其评估结果为'boolean' ?,这是真的。去掉括号 -
我真是个笨蛋。谢谢兄弟