Recognize if nodemon is started first time, not restarted

If you want in your js app to somehow recognize that it’s first time when nodemon starts (and not a restart due to script change), you might use this experimental (not reliable) solution.

    1) in package.json place such content:

    "scripts": {
      "MYSTART": "node app/app.js",
      "preMYSTART": "node -e \"const fs=require('fs'); fs.writeFileSync(__dirname + '/pre_firstrun_flag','')\""
      }
    note, the pre phrase there is for reason, it’s constant for node. so whatever you name your script (i.e. MYSTART) you should just add pre in front of it, so that command will be pre-run automatically when you run npm run MYSTART
     
    2) next, in your script you can detect with isFirstRun variable:
    const isFirstRun = fs.existsSync( 'path/to/root/pre_firstrun_flag');
    if (isFirstRun) fs.unlinkSync('path/to/root/pre_firstrun_flag');

    Leave a Comment

    Your email address will not be published. Required fields are marked *

    Puvox - Blog
    Scroll to Top