{"id":2672,"date":"2019-10-06T14:49:04","date_gmt":"2019-10-06T13:49:04","guid":{"rendered":"http:\/\/www.dpscomputing.com\/blog\/?p=2672"},"modified":"2021-03-01T21:35:18","modified_gmt":"2021-03-01T21:35:18","slug":"set-windows-environment-variables-from-command-line","status":"publish","type":"post","link":"https:\/\/www.dpscomputing.com\/blog\/2019\/10\/06\/set-windows-environment-variables-from-command-line\/","title":{"rendered":"Set Windows Environment Variables From Command Line"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"alignright\"><img loading=\"lazy\" decoding=\"async\" width=\"452\" height=\"75\" data-attachment-id=\"2674\" data-permalink=\"https:\/\/www.dpscomputing.com\/blog\/2019\/10\/06\/set-windows-environment-variables-from-command-line\/image-27\/\" data-orig-file=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2019\/10\/image-27.png\" data-orig-size=\"452,75\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image-27\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2019\/10\/image-27-300x50.png\" data-large-file=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2019\/10\/image-27.png\" tabindex=\"0\" role=\"button\" src=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2019\/10\/image-27.png\" alt=\"\" class=\"wp-image-2674\" srcset=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2019\/10\/image-27.png 452w, https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2019\/10\/image-27-150x25.png 150w, https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2019\/10\/image-27-300x50.png 300w\" sizes=\"auto, (max-width: 452px) 100vw, 452px\" \/><\/figure><\/div>\n\n\n\n<p>OK, sure there is a GUI in Windows for this &#8211; in <a href=\"https:\/\/www.youtube.com\/watch?v=bEroNNzqlF4\">Advanced &#8211; Environment Variables<\/a> accessed via the System control panel app.  <\/p>\n\n\n\n<p>But.  a) where is the fun in that?  And b) if you&#8217;re doing some development, switching to various different interfaces, keeping control panel apps open etc is a bit of a bore as well as being incredibly inefficient.  <\/p>\n\n\n\n<p>During development such as <a href=\"https:\/\/nodejs.org\/en\/\">Node.js<\/a> development, you&#8217;re probably going to want to tinker with the environment variables reasonably regularly.  <\/p>\n\n\n\n<p>What is the top change that a Node developer will want to make during development to these variables?  Answer: switching debugging on (and then off) again.  <\/p>\n\n\n\n<p>Ever spun up a server in Node and something&#8217;s gone awry? Then you might want to debug the HTTP library requests.  <\/p>\n\n\n\n<p>The examples below are setting environment variables relating to Node.js, but the commands can be used to set up &#8211; or indeed create &#8211; any environment variables of your choosing.  <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Server Example<\/h2>\n\n\n\n<p>The <a href=\"https:\/\/nodejs.org\/en\/docs\/guides\/getting-started-guide\/\">famous server example<\/a>, that has pride and place in Node&#8217;s own documentation, is what we will be looking at today.  Well, at least a variant of it &#8211; there are a few after all.  <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const http = require('http');\n\nconst server = http.createServer((req, res) =&gt; {\n    res.end('Hello World.  This is David!\\n');\n});\n\nserver.listen(4242, () =&gt; {\n    console.log('Server is running...');\n});<\/code><\/pre>\n\n\n\n<p>This is in a simple JavaScript file called &#8216;hello-world.js&#8217;.  <\/p>\n\n\n\n<p>So, if we run this without debug mode on (default), the output that we get in the console is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PS H:\\NodeCode&gt; node hello-world\nServer is running...<\/code><\/pre>\n\n\n\n<p>Open a browser, head to localhost:4242 and here&#8217;s what you should get:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"233\" height=\"74\" data-attachment-id=\"2673\" data-permalink=\"https:\/\/www.dpscomputing.com\/blog\/2019\/10\/06\/set-windows-environment-variables-from-command-line\/image-26\/\" data-orig-file=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2019\/10\/image-26.png\" data-orig-size=\"233,74\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image-26\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2019\/10\/image-26.png\" data-large-file=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2019\/10\/image-26.png\" tabindex=\"0\" role=\"button\" src=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2019\/10\/image-26.png\" alt=\"\" class=\"wp-image-2673\" srcset=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2019\/10\/image-26.png 233w, https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2019\/10\/image-26-150x48.png 150w\" sizes=\"auto, (max-width: 233px) 100vw, 233px\" \/><\/figure>\n\n\n\n<p>So, everything in this case is peachy, but what happens if it&#8217;s not?  <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Setting An Environment Variable<\/h2>\n\n\n\n<p>As previously mentioned, this example is the context of Node.js development &#8211; but can apply to any Environment Variable that you want to set for any reason what so ever.  <\/p>\n\n\n\n<p>We&#8217;re wanting to switch on debugging for the HTTP library specifically so, on the command line:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PS H:\\NodeCode&gt; $env:NODE_DEBUG=\"http\"<\/code><\/pre>\n\n\n\n<p>Simple as that!  <\/p>\n\n\n\n<p>Stop the server executing (CTRL + C twice) in the console and restart again using the previously used command.  Visit the web page in your browser &#8211; localhost:4242 and take a look at the different output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PS H:\\NodeCode&gt; node 1-hello-world\nServer is running...\nHTTP 15920: SERVER new http connection\nHTTP 15920: SERVER new http connection\nHTTP 15920: write ret = true\nHTTP 15920: outgoing message end.\nHTTP 15920: SERVER socketOnParserExecute 828\nHTTP 15920: resetHeadersTimeoutOnReqEnd\nHTTP 15920: write ret = true\nHTTP 15920: outgoing message end.\nHTTP 15920: SERVER socketOnParserExecute 749\nHTTP 15920: resetHeadersTimeoutOnReqEnd<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Clearing an Environment Variable<\/h2>\n\n\n\n<p>Again, our example is in the context of Node.js &#8211; but it&#8217;s the same for clearing any environment variable.  <\/p>\n\n\n\n<p>Head back to the command line and:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PS H:\\NodeCode&gt; $env:NODE_DEBUG=\"\"<\/code><\/pre>\n\n\n\n<p>We&#8217;re back to where we started.  Good job!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Old School Command Line<\/h2>\n\n\n\n<p>Are you on an older version of Windows?  Maybe you&#8217;re just stuck using the old school command line?  If so, please do start using PowerShell.  <\/p>\n\n\n\n<p>But, if you do insist, you&#8217;ll have to tweak the call setting the environment variable so that it works on this ye olde setup:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>H:\\NodeCode&gt;set NODE_DEBUG=\"http\"<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>OK, sure there is a GUI in Windows for this &#8211; in Advanced &#8211; Environment Variables accessed via the System control panel app. But. a) where is the fun in that? And b) if&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":2674,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"You want me to set a what on a who-what?\n\nSet Windows Environment Variables From Command Line\n\nExamples in #NodeJS\n\n#WindowsEnvrionmentVariables #DebuggingNodeJS","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[55],"tags":[2495,2489,2490,2493,2494,2496,2492,2491,2488],"class_list":["post-2672","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-articles","tag-debugging-node-environment-variables","tag-enviornment-variables","tag-environment-vars","tag-how-to-set-windows-environment-variables","tag-node-environment-variables","tag-node-environment-variables-on-windows","tag-setting-windows-enviornment-variables","tag-settings-environment-variables","tag-windows-environment-variables"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2019\/10\/image-27.png","jetpack_shortlink":"https:\/\/wp.me\/p3nsfA-H6","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/posts\/2672","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/comments?post=2672"}],"version-history":[{"count":2,"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/posts\/2672\/revisions"}],"predecessor-version":[{"id":3030,"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/posts\/2672\/revisions\/3030"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/media\/2674"}],"wp:attachment":[{"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/media?parent=2672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/categories?post=2672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/tags?post=2672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}