remy / nodemon

Monitor for any changes in your node.js application and automatically restart the server - perfect for development
http://nodemon.io/
MIT License
26.26k stars 1.72k forks source link

Restart event calls start event as well when files change #2188

Closed noctivityinc closed 6 months ago

noctivityinc commented 7 months ago

package.json - ran npm run dev

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "NODE_ENV=development nodemon devApp.js",
    "start": "nodemon index.js"
  },

devApp.js

const nodemon = require("nodemon");
const ngrok = require("ngrok");
const port = process.env.PORT || 3000;

nodemon({
  script: "index",
  ext: "js",
});

let url = null;

nodemon
  .on("start", async () => {
    console.log("nodemon start", url);
    if (!url) {
      try {
        url = await ngrok.connect({ port: port, subdomain: "whatever" });
        console.log(`Server now available at ${url}`);
      } catch (error) {
        console.log(error);
      }
    }
  })
  .on("restart", function (files) {
    console.log("App restarted due to: ", files);
  })
  .on("quit", async () => {
    await ngrok.kill();
  });

Expected behaviour

When I change a file, the restart event will be triggered.

Actual behaviour

The start event seems to be triggered, which is causing ngrok to try to reload again, which it cant because it's already running. Here is what I see in the terminal...

[nodemon] restarting due to changes...
App restarted due to:  [
  '/Users/jlippiner/Projects/qweqwe/node/server/db/models/v1/utils/AWSHelper.js'
]
[nodemon] starting `node devApp.js`

Steps to reproduce

  1. Setup ngrok
  2. Add devApp.js to your project and the correct dev line to a package.json
  3. Run npm run dev
  4. Change a file in the project

If applicable, please append the --dump flag on your command and include the output here ensuring to remove any sensitive/personal details or tokens.

node: v20.10.0
nodemon: 3.1.0
command: /Users/jlippiner/.nvm/versions/node/v20.10.0/bin/node /Users/jlippiner/Projects/visicalc/node/node_modules/.bin/nodemon devApp.js --dump
cwd: /Users/jlippiner/Projects/visicalc/node
OS: darwin x64
--------------
{
  run: false,
  system: { cwd: '/Users/jlippiner/Projects/qweqwe/node' },
  required: false,
  dirs: [ '/Users/jlippiner/Projects/qweqwe/node' ],
  timeout: 1000,
  options: {
    dump: true,
    ignore: [
      '**/.git/**',
      '**/.nyc_output/**',
      '**/.sass-cache/**',
      '**/bower_components/**',
      '**/coverage/**',
      '**/node_modules/**',
      re: /.*.*\/\.git\/.*.*|.*.*\/\.nyc_output\/.*.*|.*.*\/\.sass\-cache\/.*.*|.*.*\/bower_components\/.*.*|.*.*\/coverage\/.*.*|.*.*\/node_modules\/.*.*/
    ],
    watch: [ '*.*', re: /.*\..*/ ],
    monitor: [
      '*.*',
      '!**/.git/**',
      '!**/.nyc_output/**',
      '!**/.sass-cache/**',
      '!**/bower_components/**',
      '!**/coverage/**',
      '!**/node_modules/**'
    ],
    ignoreRoot: [
      '**/.git/**',
      '**/.nyc_output/**',
      '**/.sass-cache/**',
      '**/bower_components/**',
      '**/coverage/**',
      '**/node_modules/**'
    ],
    restartable: 'rs',
    colours: true,
    execMap: { py: 'python', rb: 'ruby', ts: 'ts-node' },
    stdin: true,
    runOnChangeOnly: false,
    verbose: false,
    signal: 'SIGUSR2',
    stdout: true,
    watchOptions: {},
    execOptions: {
      script: 'devApp.js',
      exec: 'node',
      args: [],
      scriptPosition: 0,
      nodeArgs: undefined,
      execArgs: [],
      ext: 'js,mjs,cjs,json',
      env: {}
    }
  },
  load: [Function (anonymous)],
  reset: [Function: reset],
  lastStarted: 0,
  loaded: [],
  watchInterval: null,
  signal: 'SIGUSR2',
  command: {
    raw: { executable: 'node', args: [ 'devApp.js' ] },
    string: 'node devApp.js'
  }
}
--------------
remy commented 7 months ago

@noctivityinc you're a full version behind, current latest nodemon is 3.0.2 - are you able to upgrade and then repeat your test?

github-actions[bot] commented 6 months ago

This issue has been automatically marked as idle and stale because it hasn't had any recent activity. It will be automtically closed if no further activity occurs. If you think this is wrong, or the problem still persists, just pop a reply in the comments and @remy will (try!) to follow up. Thank you for contributing <3

remy commented 6 months ago

I'll add (having looked at the code), that this is correct - the start event always runs when the subprocess is started (I've updated the wiki page for this).

The restart event is to notify that a change triggered or that a manual process was fired.

If you want to set something up for the first time, I'd recommend doing this outside of the events (or using a "start_ctr" counter or something like this).

I appreciate this probably wasn't entirely intuitive.