Difficulty with replacing the port in Gatsby JS development environment

Description of the situation

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.

This situation occurred when the structure of markdown 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

Something is already running at port 8000

? Would you like to run the app at another port instead? › (Y/n)

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.

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.

This difficult and take a lot of time which I can spend with benefit. So I began to look for the solution

Solution of the situation

So, the solution is the following, you need to partially expand package.json, which offers GatsbyJS with its command.

First of all, you need to add to your local environment a few npm packages nodemon and kill-port as the development dependencies:

npm install nodemon kill-port --save-dev

By default, nodemon is used to communicate with the Node server, and in this context, we will use its --exec property which will allow us to run commands in the terminal. For example:

nodemon --exec "your command here"

kill-port library which stops all processes on the specified port. For example:

kill-port --port 8000

By default GatsbyJS application works on 8000 port. So, before the next running of the development environment, we need to kill the 8000 port If you have another port configured, then you need to replace its value for the kill-port command.

kill-port --port 8000 && gatsby develop

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

Therefore, it makes sense to place this command in the file package.json. And it will look something like this:

// package.json

"scripts": {
    ...
    "dev": "nodemon --exec 'kill-port --port 8000 && gatsby develop' --watch gatsby-node.js --watch package.json --watch gatsby-config.js"
}

Now when you execute this command the port will be automatically "killed" and started with the updated environment

But there is last one point that is also desirable to consider when restarting the development environment, especially when changing the structure of markdown files. We need to clean the cache that generates GatsbyJS before startup the project.

For this purpose in GatsbyJS, there is a special command gatsby clean, therefore I suggest changing the command in package.json together with "killing" of port I suggest clearing a cache of our project

// package.json

"scripts": {
    ...
    "dev": "nodemon --exec 'kill-port --port 8000 && gatsby clean && gatsby develop' --watch gatsby-node.js --watch package.json --watch gatsby-config.js"
}

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

// npm
npm run dev

// yarn
yarn dev

That's all!