Create . bat file to open Node.js terminal and run one or more commands

Asked

Viewed 2,466 times

3

I would like to create a file . bat to automate a process I have to perform every day.

Whenever I start working, I open the nodejs terminal, access my project directory (in my case I use Babel) and then, I need to execute the command npm run babel.

The problem is I work on more than one project. Then sometimes I have to navigate through the terminal to the directory and then run the command to start Babel.

I would like to create a file . bat for each project, to already leave everything configured and when I click the file . Project bat I want, it opens the node terminal and execute the commands I want. This is possible?

  • It is possible yes, just create the bat file with the commands you want to run. Do you have difficulty creating the file? Just go putting the commands you want it to run, 1 per line, in order. PS if you believe you’re wasting time typing a long way into cd, maybe it is better to create shortcuts for folders in home dir.

  • Yes. I have no experience in creating this type of archive. I tried some examples I found on the internet, but I was unsuccessful. What I wanted to know is which command should I use to open the nodejs terminal and how should I place the npm commands inside that file.bat?

  • Actually, I don’t think there’s anything called the "nodejs terminal," and inside the . bat vc puts the commands exactly the same you type in cmd, it doesn’t change anything. I don’t know if I got it wrong but I added an answer, see if that’s what you need

1 answer

2


Just create a file iniciar_projeto.bat with something like:

cd C:\Users\Alan\Documentos\Seu_Projeto\
npm run babel

The first command must obviously be changed to the path of your project. This is enough to run the server if everything is set up correctly.

If . bat is on D: and your project is on drive C:, you need to change volume first:

c:
cd C:\Users\Alan\Documentos\Seu_Projeto\
npm run babel
  • Thanks man. I had tried exactly that too. But it hadn’t worked before. However, now that you posted it, I insisted on the test and found out why it went wrong the first time. I had created the.bat file in the D directory: and my test project was in C: ai whenever I ran the.bat file it didn’t roll. But after your answer I tested saving the.bat file in C: and it worked. Thanks! ;)

  • Oh yes, to change directory in Windows you have to type the letter without the cd, edited the answer to exemplify. Strange right? cmd is so messed up that Microsoft preferred to implement the linux bash instead of fixing it.

  • Yes. I also found it strange. I had already used to cd to access directories. But that’s okay. Anyway, thanks for the help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.