{"componentChunkName":"component---src-templates-blog-post-js","path":"/blog/restarting-development-environment/","result":{"data":{"markdownRemark":{"id":"b86c187a-37cf-513b-aaf3-2441ff6daf74","html":"<h5>Description of the situation</h5>\n<p>During the work with the static site based on Gatsbyjs, I got an issue. Every time when I needed to restart the development server I had a request to change the port. For example, 8000 port is busy let's try on port 8001 then port 8002, etc.</p>\n<p>This situation occurred when the structure of <code>markdown</code> files changed during the development process, which forced me to reload the environment for updating queries in GraphQL. Then in the terminal, I saw this message </p>\n<pre><code class=\"language-text\">Something is already running at port 8000\n\n? Would you like to run the app at another port instead? › (Y/n)\n</code></pre>\n<p>I agreed and the project was launched on a new port, after which I had to change the port in the browser tab or open a new one.</p>\n<p>Usually, I have open a lot of tabs on the browser. together with them I also open a tab for work with Netlify CMS. So, every change of port forced me to open a new tab. That means in my browser appear more tabs and I spend a lot of time finding a correct tab.</p>\n<p>This difficult and take a lot of time which I can spend with benefit. So I began to look for the solution </p>\n<h5>Solution of the situation</h5>\n<p>So, the solution is the following, you need to partially expand <code>package.json</code>, which offers <em>GatsbyJS</em> with its command.</p>\n<p>First of all, you need to add to your local environment a few npm packages <code>nodemon</code> and <code>kill-port</code> as the development dependencies:</p>\n<pre><code class=\"language-text\">npm install nodemon kill-port --save-dev\n</code></pre>\n<p>By default, <code>nodemon</code> is used to communicate with the <code>Node</code> server, and in this context, we will use its <code>--exec</code> property which will allow us to run commands in the terminal. For example:</p>\n<pre><code class=\"language-text\">nodemon --exec \"your command here\"\n</code></pre>\n<p><code>kill-port</code> library which stops all processes on the specified port. For example:</p>\n<pre><code class=\"language-text\">kill-port --port 8000\n</code></pre>\n<p>By default GatsbyJS application works on <code>8000</code> port. So, before the next running of the development environment, we need to kill the 8000 port\nIf you have another port configured, then you need to replace its value for the <code>kill-port</code> command.</p>\n<pre><code class=\"language-text\">kill-port --port 8000 &#x26;&#x26; gatsby develop\n</code></pre>\n<p>This is generally enough. but there is one inconvenient moment. This command we will need to enter every time when we need to restart the project. We can use the history of the terminal but it's not comfortable</p>\n<p>Therefore, it makes sense to place this command in the file <code>package.json</code>. And it will look something like this:</p>\n<pre><code class=\"language-text\">// package.json\n\n\"scripts\": {\n    ...\n    \"dev\": \"nodemon --exec 'kill-port --port 8000 &#x26;&#x26; gatsby develop' --watch gatsby-node.js --watch package.json --watch gatsby-config.js\"\n}\n</code></pre>\n<p>Now when you execute this command the port will be automatically \"killed\" and started with the updated environment</p>\n<p>But there is last one point that is also desirable to consider when restarting the development environment, especially when changing the structure of <code>markdown</code> files. We need to clean the cache that generates <em>GatsbyJS</em> before startup the project.</p>\n<p>For this purpose in  <em>GatsbyJS,</em>  there is a special command <code>gatsby clean</code>, therefore I suggest changing the command in <code>package.json</code> together with \"killing\" of port I suggest clearing a cache of our project</p>\n<pre><code class=\"language-text\">// package.json\n\n\"scripts\": {\n    ...\n    \"dev\": \"nodemon --exec 'kill-port --port 8000 &#x26;&#x26; gatsby clean &#x26;&#x26; gatsby develop' --watch gatsby-node.js --watch package.json --watch gatsby-config.js\"\n}\n</code></pre>\n<p>Ready! Now when you restart the development environment, the port will not change, and the collections for GraphQL will always be fresh. You just need to execute the following command</p>\n<pre><code class=\"language-text\">// npm\nnpm run dev\n\n// yarn\nyarn dev\n</code></pre>\n<p>That's all!</p>","frontmatter":{"title":"Difficulty with replacing the port in Gatsby JS development environment","heading":"Difficulty with replacing the port in Gatsby JS development environment","subheading":null,"templateKey":"blog-post","date":"November 21, 2020","inner_article":true,"image":{"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAHABQDASIAAhEBAxEB/8QAFwABAAMAAAAAAAAAAAAAAAAAAAIDBf/EABQBAQAAAAAAAAAAAAAAAAAAAAH/2gAMAwEAAhADEAAAAcasSAH/xAAWEAEBAQAAAAAAAAAAAAAAAAABERD/2gAIAQEAAQUCLFuf/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPwE//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAgEBPwE//8QAFBABAAAAAAAAAAAAAAAAAAAAEP/aAAgBAQAGPwJ//8QAGBABAAMBAAAAAAAAAAAAAAAAAQAQEUH/2gAIAQEAAT8hR0QuK//aAAwDAQACAAMAAAAQdA//xAAWEQEBAQAAAAAAAAAAAAAAAAABECH/2gAIAQMBAT8QTZ//xAAVEQEBAAAAAAAAAAAAAAAAAAABEP/aAAgBAgEBPxAn/8QAGhABAAMAAwAAAAAAAAAAAAAAAQARITFBYf/aAAgBAQABPxAmglZvUVtI+ZHmf//Z","aspectRatio":3,"src":"/static/23e577b774051f2d479f6a881c7fbe64/2ba53/fullsizerender.jpg","srcSet":"/static/23e577b774051f2d479f6a881c7fbe64/4d5f3/fullsizerender.jpg 240w,\n/static/23e577b774051f2d479f6a881c7fbe64/ea161/fullsizerender.jpg 480w,\n/static/23e577b774051f2d479f6a881c7fbe64/2ba53/fullsizerender.jpg 960w,\n/static/23e577b774051f2d479f6a881c7fbe64/bd316/fullsizerender.jpg 1440w,\n/static/23e577b774051f2d479f6a881c7fbe64/1be16/fullsizerender.jpg 1920w,\n/static/23e577b774051f2d479f6a881c7fbe64/1a54e/fullsizerender.jpg 3691w","sizes":"(max-width: 960px) 100vw, 960px"}}},"category":"solutions","tags":["gatsbyjs","terminal"]}},"categories":{"edges":[{"node":{"frontmatter":{"title":"Cheat Sheets"},"fields":{"slug":"/blog/category/cheat-sheets/"}}},{"node":{"frontmatter":{"title":"Solutions"},"fields":{"slug":"/blog/category/solutions/"}}},{"node":{"frontmatter":{"title":"Web Development"},"fields":{"slug":"/blog/category/web-development/"}}},{"node":{"frontmatter":{"title":"Overviews"},"fields":{"slug":"/blog/category/overviews/"}}}]}},"pageContext":{"id":"b86c187a-37cf-513b-aaf3-2441ff6daf74"}},"staticQueryHashes":["116749357","4080856488"]}