Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question] is it possible to setup nodemon to execute multiple commands watching multiple corresponding file sources? #1239

Closed
zhenyulin opened this issue Jan 31, 2018 · 11 comments

Comments

@zhenyulin
Copy link

was trying to achieve something similar by npm-scripts-watcher with nodemon
https://github.com/wehkamp/npm-scripts-watcher

@remy
Copy link
Owner

remy commented Feb 1, 2018

You can use --exec "foo & bar & car" to execute multiple commands from nodemon (though, this might not be compatible with Windows, you'll have to investigate yourself).

Other than using that method: no.

@remy remy closed this as completed Feb 1, 2018
@mzalazar
Copy link

This worked for me:
..
"start": "nodemon --exec 'clear && babel-node' src/app.js"
...

(in package.json)

@danielo515
Copy link

If I do the && trick the spawned server is not reachable. Any idea why?

@mzalazar
Copy link

mzalazar commented Jan 7, 2020

That's weird... because it's a linux common "pattern", you can try it from a linux terminal:

nodemon --exec 'clear && babel-node' src/app.js

and check for errors in the console... if that's not working for you, execute your script without nodemon:
babel-node src/app.js

and test your app.

@danielo515
Copy link

I can run it without nodemon without problems, so it has something with nodemon for sure. Maybe it is because I'm just running another npm command, not sure.
Basically what I have is:

build: "build pipeline here",
start: "run the server here",
watch: "nodemon --exec 'npm run build && npm run start'"

Which works as expected an I even see the running server on the console, but then I can not reach it. Maybe nodemon creating several children can have this effect ?

I'm quite surprised with what you proposed. Does that mean that I can pass any argument to the app?

Regards

@Jleagle
Copy link

Jleagle commented Aug 10, 2020

You can use --exec "foo & bar & car" to execute multiple commands from nodemon (though, this might not be compatible with Windows, you'll have to investigate yourself).

Other than using that method: no.

This is not compatible with long running processes

@Olliebrown
Copy link

I know things are getting a little stale here but wanted to offer up the solution I found. In general, fancy bash/*nix shell commands have caused issues for me as they don't run universally (usually cause problems for users on Windows, where the shell is, well, uncertain). The '&' and '&&' commands are a good example of this. To get around this, I've started using different 'npm' packages that offer universal alternatives.

One example is rimraf which provides a universal command to remove an entire directory (rm -rf on *nix shells).

The most relevant one to this question is npm-run-all:

  • This provides a way you can queue up several 'scripts' from your package.json to run, either in parallel or in sequence
  • run-s is provided by this package and executes the listed scripts sequentially
  • Here's a setup that builds with 'esbuild' before running with node, all monitored with nodemon:
  "scripts": {
    "dev": "nodemon --exec npm run devbuild",
    "build": "run-s clean build-bot",
    "clean": "rimraf dist",

    "devbuild": "run-s clean devbuild-bot devstart",
    "devbuild-bot": "esbuild --platform=node --bundle --outfile=dist/scrummybot.js index.js",

    "build-bot": "esbuild --platform=node --bundle --minify --outfile=dist/scrummybot.min.js index.js",

    "devstart": "node dist/scrummybot.js",
    "start": "node dist/scrummybot.min.js"
  },

The dev command starts up nodemon and nodemon just runs another package.json script ('devbuild'). Devbuild will trigger a clean, then a build, and finally starts the program. When nodemon detects a change, it kills this original process and restarts it. Works pretty well for me when I have things that need to be built first (usually because they are using ES Next type features that node doesn't support).

@yucongo
Copy link

yucongo commented Mar 16, 2021

"start": "nodemon --exec 'clear && babel-node' src/app.js" single quotes do not work in Windows. Double quotes do (need to escape " inside)

`"start": "nodemon --exec \"clear && babel-node\" src/app.js"`

@davidmroth
Copy link

davidmroth commented Mar 21, 2021

Here's is what I ended up with that's working relatively well:

In my package.json I added to the scripts key:

"develop": "npm i nodemon; nodemon --exec \"npx prettier --write '{,./src/}{,*/**/}*.{js,json}' && node src/server.js\"",

Now I can run npm run develop and Nodemon will prettify my src and run my server.

@santimirandarp
Copy link

santimirandarp commented Jan 21, 2022

This set up was quite useful to me, running eslint and prettier in typescript (may add the tests also):

 "scripts": {
       "compile": "tsc -b",
       "eslint": "eslint src/**/*.ts --cache",
       "prettierw": "prettier src/**/*.ts --write",
       "dev": "nodemon -e ts --watch src/ --exec 'npm run eslint && npm run prettierw' "
    },

Good to emphasize that -e ts is necessary as that's not watched by default.

I can't pollute the project files but I may add it in a upper level folder =)

@iradofurioso
Copy link

"start": "nodemon --exec 'clear && babel-node' src/app.js" single quotes do not work in Windows. Double quotes do (need to escape " inside)

`"start": "nodemon --exec \"clear && babel-node\" src/app.js"`

Thanks, it did work for me. I want to make my api compatible to all environments and that double quotes scaped did the trick.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants