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

福尔摩斯对我笑 2019-01-16 28350 次 9183条

配置环境变量

首先明确一点:所有的在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
未来博客 博主大人
到了这个年纪,不应该再彷徨!

发表评论



 


已有 9183 条评论 1


  1. moncler jackets

    I and my buddies appeared to be going through the nice ideas located on your site and so unexpectedly I got a horrible feeling I never expressed respect to you for those secrets. Those ladies appeared to be for that reason excited to read through them and have now in truth been tapping into those things. Many thanks for being so considerate and also for obtaining variety of essential subject areas most people are really needing to know about. Our own honest regret for not expressing gratitude to sooner.

    回复 2019年10月27日
  2. kobe shoes

    Thank you so much for providing individuals with an extremely spectacular chance to discover important secrets from this site. It is always so great and also jam-packed with amusement for me personally and my office friends to visit the blog not less than thrice weekly to learn the fresh items you have. Not to mention, I’m always contented with all the spectacular information you serve. Some two facts in this article are easily the best we have all ever had.

    回复 2019年10月27日
  3. golden goose outlet

    I and my friends ended up reading the best tips and hints on your web blog and so suddenly I had an awful feeling I had not thanked the website owner for those strategies. My guys ended up so happy to read them and have now in actuality been taking pleasure in these things. Thank you for truly being very kind and for opting for these kinds of superior tips millions of individuals are really desperate to be aware of. My sincere apologies for not expressing appreciation to you sooner.

    回复 2019年10月26日
  4. jordan shoes

    I together with my pals were checking out the best tricks found on your web site then at once came up with a terrible feeling I had not expressed respect to the web blog owner for those secrets. All of the people were definitely for that reason passionate to read all of them and now have surely been having fun with these things. Thank you for truly being so thoughtful as well as for utilizing these kinds of fine resources millions of individuals are really eager to learn about. Our honest apologies for not expressing appreciation to you earlier.

    回复 2019年10月26日
  5. yeezy boost 350

    I definitely wanted to make a quick word so as to appreciate you for some of the fabulous tricks you are showing here. My time-consuming internet lookup has at the end of the day been rewarded with reputable content to go over with my pals. I ‘d point out that most of us site visitors are very lucky to dwell in a fabulous place with many outstanding people with useful opinions. I feel rather lucky to have encountered the weblog and look forward to really more enjoyable moments reading here. Thank you once more for all the details.

    回复 2019年10月26日
  6. jordan shoes

    My husband and i ended up being comfortable Jordan managed to carry out his inquiry out of the ideas he acquired out of your web site. It’s not at all simplistic to just continually be releasing strategies that some people could have been making money from. And we fully grasp we’ve got the website owner to thank for this. The main illustrations you’ve made, the straightforward website menu, the relationships you make it possible to create – it is all powerful, and it is making our son and our family consider that the content is brilliant, and that is extremely mandatory. Many thanks for the whole thing!

    回复 2019年10月25日
  7. golden goose sneakers

    I have to point out my respect for your kind-heartedness supporting visitors who should have help on this particular question. Your real dedication to passing the solution all over came to be unbelievably useful and have constantly allowed many people like me to arrive at their endeavors. Your personal important key points signifies much a person like me and further more to my office workers. Regards; from each one of us.

    回复 2019年10月25日
  8. mbt shoes

    I actually wanted to construct a small note in order to thank you for some of the wonderful items you are giving at this site. My prolonged internet research has now been honored with good know-how to exchange with my companions. I ‘d assume that many of us visitors are very much lucky to exist in a superb network with so many wonderful professionals with good ideas. I feel really blessed to have used your entire web pages and look forward to tons of more exciting times reading here. Thank you once again for a lot of things.

    回复 2019年10月25日
  9. coach outlet

    I’m also commenting to make you understand what a great encounter my friend’s girl undergone checking yuor web blog. She mastered a good number of details, which include what it’s like to have an awesome helping mindset to get many others easily learn about some extremely tough topics. You truly exceeded our expected results. Many thanks for producing those practical, healthy, edifying and as well as cool tips on that topic to Mary.

    回复 2019年10月25日
  10. nike outlet

    I precisely needed to thank you very much all over again. I am not sure the things I could possibly have followed without those creative concepts contributed by you directly on my area of interest. It truly was the terrifying condition in my position, however , finding out a new expert approach you treated it forced me to cry for contentment. Now i am thankful for the guidance and in addition believe you know what an amazing job you’re undertaking teaching the rest through your webpage. Most probably you’ve never got to know any of us.

    回复 2019年10月24日
  11. cheap jordans

    I wanted to send you that very small note to say thanks the moment again for all the amazing strategies you have discussed above. It was so unbelievably open-handed with people like you to convey extensively just what most people might have advertised for an e-book in making some profit for their own end, precisely given that you could possibly have tried it in the event you desired. Those advice as well served like a great way to realize that other people have a similar eagerness the same as my own to realize a little more with regard to this problem. I know there are many more pleasurable sessions in the future for those who read through your website.

    回复 2019年10月24日
  12. nike air max 270

    I as well as my friends were found to be taking note of the best guidelines from your web page while unexpectedly I got a horrible feeling I had not expressed respect to the website owner for those tips. Those ladies had been as a consequence stimulated to study all of them and have sincerely been taking pleasure in these things. I appreciate you for turning out to be very kind and for deciding upon varieties of terrific guides millions of individuals are really desperate to discover. My very own honest regret for not expressing gratitude to you earlier.

    回复 2019年10月24日
  13. kyrie 4

    Thanks for all of your hard work on this web page. My niece delights in doing investigation and it’s really easy to understand why. Many of us notice all concerning the powerful medium you give important strategies through your website and therefore cause response from other people about this point then our girl is really becoming educated so much. Take pleasure in the rest of the new year. You are doing a really great job.

    回复 2019年10月24日
  14. converse shoes

    My husband and i felt comfortable Emmanuel managed to do his studies using the precious recommendations he got when using the blog. It’s not at all simplistic just to choose to be making a gift of ideas that many the rest could have been trying to sell. So we understand we now have the writer to thank because of that. The main explanations you made, the straightforward blog menu, the friendships you can help to engender – it’s many extraordinary, and it is leading our son in addition to the family believe that this article is fun, and that is pretty fundamental. Thanks for all!

    回复 2019年10月23日
  15. lebron 16

    I enjoy you because of all your valuable labor on this blog. Ellie take interest in engaging in internet research and it’s really easy to see why. My partner and i learn all concerning the lively medium you offer practical information by means of your web site and recommend participation from others on that content so my princess is certainly learning a great deal. Enjoy the rest of the year. You’re the one conducting a stunning job.

    回复 2019年10月23日
  16. golden goose outlet

    I needed to post you that little observation in order to say thanks a lot over again for all the splendid strategies you have contributed in this article. This has been so particularly generous of you to supply openly what exactly most people would have distributed for an ebook to make some money for themselves, specifically now that you might well have done it in the event you desired. Those strategies as well worked to become a great way to understand that other people have a similar zeal just like my very own to see somewhat more around this matter. Certainly there are numerous more pleasant sessions in the future for individuals who looked at your blog post.

    回复 2019年10月22日
  17. adidas tubular

    Thank you so much for giving everyone an extraordinarily special possiblity to check tips from this web site. It is always very terrific and also packed with a great time for me personally and my office acquaintances to search your web site more than 3 times a week to study the new secrets you will have. And definitely, I am also always motivated for the brilliant hints served by you. Selected 2 ideas in this article are basically the most effective I have ever had.

    回复 2019年10月21日
  18. StevDede

    Le Viagra Et Autres Buy Kamagra Quick Cephalexin Kidney In Cats cialis Amoxicillin And Vitamin And Headache Viagra Foto Divertenti Levitra Ersatz Online

    回复 2019年10月21日
  19. longchamp outlet

    I needed to compose you the tiny note in order to thank you the moment again with the pleasing advice you’ve provided in this case. It’s quite unbelievably open-handed of people like you to make unhampered precisely what most people might have marketed as an e book to make some money on their own, primarily seeing that you might have tried it if you considered necessary. The things also served to be a fantastic way to comprehend other individuals have the same keenness really like my personal own to find out a good deal more in respect of this matter. I’m certain there are many more pleasant sessions in the future for individuals that take a look at your blog.

    回复 2019年10月20日
  20. curry shoes

    Thank you for each of your labor on this site. My mother take interest in making time for investigations and it is obvious why. We learn all of the lively mode you produce both interesting and useful things by means of the web blog and therefore cause response from visitors on this area so our child is truly starting to learn so much. Enjoy the remaining portion of the new year. You’re conducting a useful job.

    回复 2019年10月19日
  21. balenciaga sneakers

    I’m writing to let you be aware of of the awesome encounter my friend’s princess encountered browsing your web site. She noticed a good number of things, not to mention what it is like to have an incredible helping style to let other folks really easily know selected grueling things. You actually surpassed our own expected results. Thanks for churning out such valuable, trusted, explanatory and in addition easy tips about this topic to Kate.

    回复 2019年10月18日
  22. ralph lauren sale

    Thank you a lot for giving everyone remarkably terrific chance to check tips from here. It’s always so pleasant and as well , stuffed with fun for me personally and my office colleagues to visit your website not less than three times in a week to see the new secrets you will have. And definitely, I’m so usually happy with all the splendid ideas served by you. Selected two points in this posting are definitely the most impressive we have ever had.

    回复 2019年10月18日
  23. moncler outlet

    I am glad for commenting to let you understand what a incredible discovery my friend’s girl experienced studying your blog. She came to understand numerous pieces, which include what it is like to have an ideal helping style to let many people without problems know precisely some hard to do matters. You undoubtedly did more than readers’ desires. I appreciate you for coming up with these powerful, healthy, educational and even cool thoughts on this topic to Evelyn.

    回复 2019年10月17日
  24. vapormax

    I wish to express thanks to you just for bailing me out of this type of challenge. Because of looking out through the online world and seeing ideas which are not beneficial, I believed my entire life was done. Being alive minus the approaches to the issues you’ve solved all through your good post is a serious case, as well as the ones that would have negatively affected my career if I had not encountered the blog. Your main know-how and kindness in touching a lot of stuff was very helpful. I don’t know what I would have done if I had not discovered such a subject like this. It’s possible to at this time relish my future. Thanks very much for this reliable and results-oriented help. I will not be reluctant to recommend your blog to anyone who needs to have guide on this subject.

    回复 2019年10月16日
  25. air max 95

    I just wanted to construct a brief remark in order to appreciate you for all of the stunning tips and hints you are posting here. My time-consuming internet lookup has now been rewarded with incredibly good ideas to write about with my company. I ‘d point out that many of us website visitors actually are very much endowed to be in a very good network with many special professionals with great tips and hints. I feel very much blessed to have used your webpage and look forward to really more fabulous times reading here. Thank you again for all the details.

    回复 2019年10月15日
  26. yeezy shoes

    I would like to express my appreciation to this writer for bailing me out of this difficulty. Right after researching throughout the search engines and finding thoughts which were not powerful, I believed my life was well over. Existing minus the solutions to the problems you have solved all through this short article is a crucial case, as well as ones that might have in a negative way affected my career if I had not encountered the website. Your personal expertise and kindness in dealing with all things was precious. I don’t know what I would have done if I hadn’t come upon such a subject like this. It’s possible to now look forward to my future. Thank you so much for the professional and amazing help. I will not think twice to propose your web sites to any person who should get guidance on this subject matter.

    回复 2019年10月14日
  27. coach outlet store

    I’m writing to make you know what a useful encounter our girl encountered visiting the blog. She mastered such a lot of pieces, including what it’s like to possess an incredible giving character to get many others clearly learn about selected advanced subject matter. You undoubtedly did more than my desires. Thanks for distributing these interesting, dependable, educational not to mention easy tips on the topic to Jane.

    回复 2019年10月13日
  28. 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日
  29. 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日
  30. 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日
  31. 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日
  32. 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日
  33. 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日