Nodemon: Support for npm start script

Created on 28 Jan 2013  路  14Comments  路  Source: remy/nodemon

Hey there,

Is there any way of using nodemon with npm's start script?

I tried:

$ nodemon -x npm start

and

$ nodemon -x "npm start"

and also just nodemon on it's own, but I just get the help dialogue with no error.

Most helpful comment

Here was my solution:

"scripts": {
    "prestart": "eslint .",
    "start": "node app",
    "dev": "nodemon --exec npm start"
  },

Nodemon will automatically catch any errors with eslint and then restart on changes.

All 14 comments

Wouldn't you rather put

"scripts": {
  "start": "nodemon app"
}

in your package.json and call it with npm start.
That of course means you'll have to pass what's in your start target currently to nodemon, but that's a different issue.

Not a massive fan of that, as I use the same start script on development and production environments.

nodemon checks if the "app.js" you want to launch exists as a file. So in order to run nodemon -x npm start, create a file named "start". I ran into trouble, when the nodemon couldn't terminate the process so as to release the server socket in time, however.
I tried, and the same happens with the "supervisor" package.

If your start parameters are complex, why not incorporate them in a .js-script of their own?

Yeh, that's probably the way to go here. I was originally using Cake, but I guess I can probably have a re-think.

I would also like to use nodemon to run $ npm start because I want it to also run the prestart script which lints my project.

Example:

// package.json snippet
"scripts": {
    "prestart": "eslint index.js",
    "start": "node index.js"
}

if i run $ npm start, it first runs $ npm prestart.
I want to somehow do this with nodemon so that it runs not only the start script but also the prestart script.

Any ideas?

The pre and post hooks for npm aren't supported. The solution here would be to change the start value to nodemon index.js.

That won't work exactly for me b/c I used npm start in production.

But that did spark a light bulb. I can do something like this:

"prestart": "eslint .",
    "start": "node index.js",
    "predev": "eslint .",
    "dev": "nodemon index.js"
}

and then $ npm start will work for prod and $ npm run dev will work for development.

BOOYA!

Thanks for your help.

Crap i spoke too soon. It only runs the predev script on initial launch. Subsequent file changes only run nodemon again and not the predev npm script.

Here was my solution:

"scripts": {
    "prestart": "eslint .",
    "start": "node app",
    "dev": "nodemon --exec npm start"
  },

Nodemon will automatically catch any errors with eslint and then restart on changes.

@nathanhleung Does that mean eslint will run even in production via "npm start" instead of "npm dev"?

@VictorioBerra yes, since the prestart hook will run eslint. However, eslint will only run once if you invoke npm start directly (with the aforementioned config).

Alternatively, you could try this approach:

"scripts": {
    "predev": "eslint .",
    "dev": "npm start",
    "start": "node app",
    "nodemon": "nodemon --exec npm dev"
  },

You'd run npm run nodemon to lint on changes, and npm start directly to run the server without linting.

@nathanhleung I needed to add run into npm dev for npm run dev

Are there hooks we can put in nodemon.json instead of npm scripts?

the following npm start works fine for me

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon ./server.js localhost 8080"
  }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

robboerman picture robboerman  路  3Comments

remy picture remy  路  5Comments

jonerer picture jonerer  路  4Comments

Bastorx picture Bastorx  路  5Comments

Makoehle picture Makoehle  路  3Comments