Javascript function making nodemon run in loop

Asked

Viewed 100 times

0

I made a function that works when run from node app.js, but when I run with Nodemon it starts and reboots forever.

The function in question is used to write a JSON to a file. I need to use Nodemon because it greatly optimizes my time.

Detail: when I use another file format, .txt for example, it works normally, but I need the file to be in format .json because I will store the data Baco information in it.

My job:

async function grava_login(){                          
 fs.writeFile("data.json", JSON.stringify({name: "Mohamed Almaci"}),
 "utf8", async function grava_login(err){
  try{                                                       
   if(err){                                                
    console.log(err);                                     
   }                                                      
   else{                                                   
    console.log("The file was saved!");                  
   }                                                     
 }                                                      
 catch{                                                  
  console.log("Erro ao salvar dados no arquivo json!")  
 }                                                     
})}                                                    
grava_login()
  • It should be "restarting forever" because your script always writes a new file in a folder that is probably watched by Listener nodemon...

  • I tried to change more didn’t work, but the strange thing is that in other file formats works :(

1 answer

2


According to the documentation from the Nodemon library, if no configuration is provided for the library it will observe the following file types: .js, .mjs, .coffee, .litcoffee, and .json.

This means that the Nodemon process will check any changes made to files with these extensions. When one of these files changes, it recurs the process.

What’s going on with your code

The function grava_login will write in the file data.json, inside the root directory the contents {"name": "Mohamed Almaci"}.

When this happens the Nodemon process will understand that a file with extension .json has been modified and will restart your application. When the application restarts, it will call the function grava_login. Thus creating an infinite cycle of executions of your application.

How to solve the problem

To documentation informs that it is possible to create a configuration file of Nodemon. To do this, simply create a file with the name nodemon.json at the root of your project. With it, you can specify the file types and directories to be "observed". In your case, include in this file the property: "ignore": ["*.json"] should solve the problem.

Example of how your file should look nodemon.json

{
  "ignore": ["*.json"]
}
  • Good boy, I knew what was going on but I didn’t know I was going to have to create a json configuration file. I figured I would have to read the documentation (which is usually great there posted on Stack overflow), the message was in my face when I started the nodemon actually kkk, the message I was displaying when running the nodemon:

  • [nodemon] 2.0.6 [nodemon] to Restart at any time, enter rs [nodemon] watching path(s): . [nodemon] watching Extensions: js,mjs,json

  • It was my lack of attention, thank you

Browser other questions tagged

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