node通过环境变量控制是否为开发环境

福尔摩斯对我笑 2019-01-16 47836 次 11206条

配置环境变量

首先明确一点:所有的在cmd命令行下对环境变量的修改只对当前窗口有效,不是永久性的修改。也就是说当关闭此cmd命令行窗口后,将不再起作用。永久性修改环境变量的方法有两种:一种是直接修改注册表(此种方法目前没试验过),另一种是通过我的电脑-〉属性-〉高级,来设置系统的环境变量(查看详细)。

临时修改:
直接在cmd环境配置即可,查看环境变量,添加环境变量,删除环境变量。

#node中常用的到的环境变量是NODE_ENV,首先查看是否存在
set NODE_ENV
#如果不存在则添加环境变量
set NODE_ENV=production
#环境变量追加值 set 变量名=%变量名%;变量内容
set path=%path%;C:web;C:Tools
#某些时候需要删除环境变量
set NODE_ENV=

永久配置右键(此电脑) -> 属性(R) -> 高级系统设置 -> 环境变量(N)...

可通过下面的代码控制环境,这样只需要修改环境变量就能灵活切换代码运行环境了

if( process.env.NODE_ENV == "production"){
console.log("这是开发环境")
}

我再VScode中获取不到设置的环境变量,直接在目录中却可以。

上面的方法由于电脑设置的不同,可能出现异常,这里使用cross-env模块解决这个问题。

npm install --save-dev cross-env

package.json文件进行配置

index.js 是我的启动文件,development可随你修改

"development": "cross-env NODE_ENV=development node index.js",
"production": "cross-env NODE_ENV=production node index.js"

这样我们启动时可以区分哪个环境了
npm run production 生产环境
npm run development 开发环境

标签: 无

非特殊说明,本博所有文章均为博主原创。

头像也是LOGO
未来博客 博主大人
到了这个年纪,不应该再彷徨!

发表评论



 


已有 11206 条评论 1


  1. supreme hoodie

    I want to convey my admiration for your generosity for those individuals that must have guidance on this subject. Your special dedication to passing the solution along came to be extremely beneficial and has really empowered professionals just like me to attain their ambitions. Your entire important publication can mean a great deal to me and much more to my colleagues. Best wishes; from each one of us.

    回复 2019年10月12日
  2. fila disruptor

    I needed to post you one little bit of remark just to say thank you as before just for the amazing views you’ve shared on this page. It was really unbelievably open-handed of you to offer publicly exactly what a number of people would have offered for an ebook in order to make some cash for their own end, particularly considering the fact that you could have done it if you ever decided. These ideas in addition worked as a great way to know that some people have the identical dreams just like my personal own to learn good deal more in regard to this issue. I’m certain there are numerous more pleasurable occasions ahead for individuals who looked over your blog.

    回复 2019年10月11日
  3. nike air max

    I am glad for writing to let you understand what a fabulous experience my friend’s girl gained checking your web site. She picked up numerous issues, which included what it is like to have a marvelous teaching mindset to make many others easily know precisely a number of complex things. You undoubtedly did more than her desires. Many thanks for giving these effective, dependable, educational and also cool tips on your topic to Gloria.

    回复 2019年10月10日
  4. harden shoes

    Thank you for each of your hard work on this blog. My mum delights in carrying out investigations and it’s really obvious why. All of us learn all about the dynamic form you offer useful techniques on the web site and in addition attract participation from the others on this article so our child is undoubtedly learning so much. Take advantage of the remaining portion of the new year. You have been conducting a superb job.

    回复 2019年10月9日
  5. nike air max

    I wish to show my appreciation for your kind-heartedness supporting men and women who have the need for assistance with the issue. Your real commitment to passing the message all-around had become extraordinarily beneficial and have always enabled girls like me to arrive at their objectives. The valuable guide means a great deal to me and even more to my office colleagues. Best wishes; from everyone of us.

    回复 2019年10月9日
  6. cheap jordans

    I intended to compose you the very little note so as to thank you so much yet again relating to the extraordinary guidelines you’ve shared above. This has been tremendously generous with you to make openly all many of us might have marketed for an e book in making some dough for themselves, especially now that you might have tried it in the event you considered necessary. The tricks likewise served to be a great way to comprehend other individuals have similar interest just as my very own to see a whole lot more when it comes to this problem. I am certain there are numerous more enjoyable opportunities up front for individuals who looked over your blog post.

    回复 2019年10月8日