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

福尔摩斯对我笑 2019-01-16 5682 次 1857条

配置环境变量

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

发表评论



 


已有 1857 条评论 1


  1. w88

    I think the admin of this website is truly working hard in favor of his web
    page, because here every data is quality based material.

    回复 2020年2月26日
  2. gay porn

    Hello there, I think your site might be having browser compatibility problems.
    When I look at your website in Safari, it looks fine however when opening in I.E., it has some overlapping issues.
    I simply wanted to give you a quick heads up!
    Besides that, wonderful site!

    回复 2020年2月26日
  3. dummy

    Aw, this was a really nice post. Spending some time and actual effort to create a top notch article… but what can I say… I hesitate a whole lot and don’t seem to get nearly anything done.

    回复 2020年2月26日
  4. erection

    Way cool! Some extremely valid points! I appreciate you writing this write-up and also the rest of the website is also really good.

    回复 2020年2月26日
  5. hore

    This is a really good tip particularly to those fresh to the blogosphere. Short but very precise info… Thank you for sharing this one. A must read post!

    回复 2020年2月26日
  6. jasa pembuatan sbu

    Your style is so unique in comparison to other folks I have read stuff from. Thanks for posting when you’ve got the opportunity, Guess I will just book mark this web site.

    回复 2020年2月26日
  7. bastard

    I was able to find good info from your blog articles.

    回复 2020年2月26日
  8. cockfucker

    I’m amazed, I must say. Seldom do I come across a blog that’s both educative and interesting, and without a doubt, you have hit the nail on the head. The issue is an issue that not enough men and women are speaking intelligently about. I’m very happy that I found this in my search for something concerning this.

    回复 2020年2月26日
  9. Click Here

    I blog often and I really appreciate your information. The article has truly peaked my interest. I am going to bookmark your blog and keep checking for new information about once per week. I opted in for your Feed as well.

    回复 2020年2月26日
  10. 영화

    It is not my first time to visit this site, i
    am visiting this web page dailly and get good facts from here
    everyday.

    回复 2020年2月26日
  11. suck

    I’m impressed, I must say. Rarely do I come across a blog that’s both equally educative and entertaining, and without a doubt, you have hit the nail on the head. The problem is something too few folks are speaking intelligently about. I am very happy I stumbled across this during my hunt for something concerning this.

    回复 2020年2月26日
  12. darn

    Everything is very open with a really clear clarification of the challenges. It was really informative. Your website is very helpful. Thanks for sharing!

    回复 2020年2月26日
  13. boong

    This is a really good tip especially to those fresh to the blogosphere. Short but very accurate information… Many thanks for sharing this one. A must read post!

    回复 2020年2月26日
  14. kobe sneakers

    I’m also writing to make you be aware of what a great discovery my friend’s daughter experienced using your web page. She realized many things, which include what it’s like to have an excellent coaching heart to have folks clearly have an understanding of chosen multifaceted subject areas. You truly surpassed visitors’ expected results. Thank you for imparting these practical, trustworthy, informative and fun guidance on your topic to Jane.

    回复 2020年2月26日
  15. Fuck off

    Oh my goodness! Impressive article dude! Many thanks, However I am encountering issues with your RSS. I don’t understand the reason why I am unable to join it. Is there anyone else having identical RSS issues? Anyone who knows the solution can you kindly respond? Thanx!!

    回复 2020年2月26日
  16. bastard

    This site really has all the info I needed concerning this subject and didn’t know who to ask.

    回复 2020年2月26日
  17. black cock

    That is a great tip particularly to those new to the blogosphere. Simple but very accurate info… Appreciate your sharing this one. A must read post!

    回复 2020年2月26日
  18. Abbruch aus Lübeck

    I’m impressed, I have to admit. Rarely do I encounter a blog that’s equally educative and engaging, and without a doubt, you have hit the nail on the head. The problem is something that not enough people are speaking intelligently about. I’m very happy I stumbled across this during my hunt for something relating to this.

    回复 2020年2月26日
  19. ass

    This site was… how do you say it? Relevant!! Finally I have found something which helped me. Many thanks!

    回复 2020年2月26日
  20. Завтрак бургер

    I’ve been exploring for a little for any high quality articles or blog posts on this sort of
    area . Exploring in Yahoo I finally stumbled upon this web site.
    Studying this info So i am happy to convey that I have an incredibly good uncanny
    feeling I found out just what I needed. I so much definitely will make
    sure to don?t forget this website and provides it a glance
    regularly.

    回复 2020年2月26日
  21. hardcore

    I was able to find good advice from your blog articles.

    回复 2020年2月26日
  22. penis

    I really like it when individuals come together and share ideas. Great website, continue the good work!

    回复 2020年2月26日
  23. ninh bình

    Hmm it looks like your blog ate my first comment (it was super
    long) so I guess I’ll just sum it up what I submitted and say, I’m thoroughly enjoying your blog.
    I too am an aspiring blog blogger but I’m still new to everything.
    Do you have any recommendations for first-time blog writers?
    I’d genuinely appreciate it.

    回复 2020年2月26日
  24. Raleigh NC

    Right here is the right website for everyone who really wants to find out about this topic. You know so much its almost hard to argue with you (not that I really would want to…HaHa). You definitely put a brand new spin on a subject that’s been discussed for many years. Wonderful stuff, just wonderful.

    回复 2020年2月26日
  25. 베스트 카지노

    I’ve been browsing online more than three hours nowadays,
    but I never found any attention-grabbing article like
    yours. It’s beautiful value sufficient for me.
    In my view, if all website owners and bloggers made excellent content material as you probably did, the
    web shall be much more useful than ever before.

    回复 2020年2月26日
  26. Extension Cord AWG

    I have learn a few good stuff here. Definitely price bookmarking for revisiting.
    I surprise how much effort you place to create this sort of great informative website.

    回复 2020年2月26日
  27. pornography

    Great blog you’ve got here.. It’s hard to find quality writing like yours these days. I honestly appreciate people like you! Take care!!

    回复 2020年2月26日
  28. 예스카지노

    This is breaking today.

    ब्रेकिंग न्यूज़ आज की।

    Rafale … https://uricasino369.com/

    回复 2020年2月26日
  29. display walls

    Hi my family member! I wish to say that this post is awesome, nice written and include almost all vital infos.

    I’d like to see more posts like this .

    回复 2020年2月26日
  30. saç ektirme fiyatları

    I blog often and I really thank you for your content.

    Thiss great article has really peaked my interest.
    I will book mak your blog and keep checking for new details about once per week.
    I opted in for your RSS feed too.

    回复 2020年2月26日
  31. pomysłowa

    If you wish for to take a great deal from this piece of writing then you have
    to apply these strategies to your won blog.

    回复 2020年2月26日
  32. bloody hell

    I blog often and I really thank you for your information. Your article has really peaked my interest. I will book mark your site and keep checking for new information about once a week. I opted in for your Feed as well.

    回复 2020年2月26日
  33. ocean king quepos telefono

    I have been surfing online more than 3 hours today, yet I never found any interesting article like yours.

    It is pretty worth enough for me. In my opinion, if all webmasters and bloggers
    made good content as you did, the internet will be a lot more useful than ever before.

    回复 2020年2月26日
  34. Baharat tohumu

    What’s up Dear, are you actually visiting this
    website daily, if so after that you will definiterly obtain god experience.

    回复 2020年2月26日
  35. astm f1852

    I’m not sure exactly why but this website is loading extremely slow for me. Is anyone else having this problem or is it a problem on my end? I’ll check back later on and see if the problem still exists.

    回复 2020年2月26日
  36. usb wifi cho pc

    Nhà phân phối các sản phẩm: PCMAX Máy trợ giảng, Máy lọc không khí, Bộ chia tín hiệu HDMI,
    Cáp HDMI, Ổ điện âm tường, âm sàn, HDMI không dây, Bộ chia HDMI, Bộ gộp HDMI, Bộ ghép màn hình

    回复 2020年2月26日
  37. www

    Hi there, You have done an excellent job. I’ll definitely digg it
    and personally recommend to my friends. I am confident they’ll be benefited from this web site.

    回复 2020年2月25日
  38. damn

    I’d like to thank you for the efforts you have put in penning this site. I am hoping to check out the same high-grade blog posts from you in the future as well. In truth, your creative writing abilities has encouraged me to get my very own website now 😉

    回复 2020年2月25日
  39. w88

    Please let me know if you’re looking for a writer for your blog.
    You have some really great articles and I feel I would be a good
    asset. If you ever want to take some of the load off, I’d really like to write
    some content for your blog in exchange for a link back
    to mine. Please blast me an e-mail if interested. Regards!

    回复 2020年2月25日
  40. modern era construction inc

    I know this if off topic but I’m looking into starting my own blog and was curious what all is required to get set up? I’m assuming having a blog like yours would cost a pretty penny? I’m not very web smart so I’m not 100% positive. Any recommendations or advice would be greatly appreciated. Many thanks

    回复 2020年2月25日
  41. Play Lotto

    The next time I read a blog, I hope that it won’t fail me just as much as this particular one. After all, Yes, it was my choice to read through, but I truly believed you would have something helpful to talk about. All I hear is a bunch of crying about something that you could fix if you were not too busy looking for attention.

    回复 2020年2月25日
  42. Ghana worship songs

    Hi there, just became aware of your blog through Google, and found that it
    is truly informative. I am gonna watch out for brussels.

    I will appreciate if you continue this in future. Numerous people will be benefited from your writing.
    Cheers!

    回复 2020年2月25日
  43. forex forum

    This is very interesting, You’re a very skilled blogger.
    I have joined your rss feed and look forward to seeking more of your
    fantastic post. Also, I’ve shared your web site in my social networks!

    回复 2020年2月25日
  44. http://pestexterminators.info/category/eastside-pest-control/

    An impressive share! I’ve just forwarded this onto a
    colleague who had been conducting a little homework on this.
    And he in fact ordered me dinner due to the fact that I stumbled upon it for him…
    lol. So let me reword this…. Thank YOU for the meal!!
    But yeah, thanks for spending the time to discuss this
    issue here on your website.

    回复 2020年2月25日
  45. 금융

    I all the time emailed this website post page to
    all my associates, for the reason that if like to read
    it afterward my contacts will too.

    回复 2020年2月25日
  46. date

    Pretty! This has been a really wonderful post. Many thanks for supplying these details.

    回复 2020年2月25日
  47. hdmi ra vga

    Very great post. I just stumbled upon your blog and wanted
    to mention that I have truly enjoyed browsing your weblog posts.
    In any case I’ll be subscribing for your
    feed and I’m hoping you write again very soon!

    回复 2020年2月25日
  48. Lexus888 V2

    Awesome article.

    回复 2020年2月25日
  49. Fayetteville NC

    I was able to find good info from your blog articles.

    回复 2020年2月25日
  50. chatbulate

    Dije casita, más familia con meme unida y camping todo el verano y las
    infaltables

    回复 2020年2月25日