86

I know how to do nodemon server.js but what if I want to do nodemon ./src

I want restart node on any changes in the directory of src.

When I do above and it say cannot find module babelprac\src

I am also doing in another command window : npm run scripts:watch

The script is

"scripts" : {
  "scripts" : "babel src --source-maps-inline --out-dir dist",
  "scripts:watch" : "babel src --watch --source-map-inline --out-dir dist"
},

That runs the watch but I want to run the script in src or dist to see the console.logs

I aslo tried nodemon --watch ./src. It says it can't find index.js.

I am on windows 7

My working directory is babelprac

vinS
  • 1,417
  • 5
  • 24
  • 37
jack blank
  • 5,073
  • 7
  • 41
  • 73

5 Answers5

103

Nodemon expects it just as:

nodemon --watch src server.js

https://github.com/remy/nodemon#monitoring-multiple-directories

nodemon --watch app --watch libs app/server.js

Kody
  • 1,319
  • 1
  • 9
  • 12
  • It say at the begining of the doc `nodemon will watch the files in the directory in which nodemon was started` if I use server.js it seems to me like it will watch server.js not the directory. I want it to watch the directory. – jack blank Jan 18 '17 at 21:35
  • and `--watch app` signature doesn't work for me. I read that page before asking this question. – jack blank Jan 18 '17 at 21:37
  • 2
    @jackblank No, you are telling nodemon the script to run which is `server.js`. That line means that nodemon will watch all files and directories where you run `nodemon server.js` from, which is where you are starting nodemon. – Kody Jan 18 '17 at 21:37
  • @jackblank Did you try `nodemon --watch src server.js` you have to replace `--watch DIR` with the directory you would like to be watched for changes, app is an example. – Kody Jan 18 '17 at 21:38
  • I tried `C:\Users\jack\Desktop\practice\stackPrac\jsPrac\babelprac>nodemon --watch src main.js` – jack blank Jan 18 '17 at 21:40
  • It says `Error: Cannot find module 'C:\Users\jack\Desktop\practice\stackPrac\jsPrac\babel prac\main.js'` For some reason it doesn't recognize src directory now. I have it in babelprac. – jack blank Jan 18 '17 at 21:40
  • @jackblank does the file `main.js` exist? You need to put the nodeJS script you want to run there. – Kody Jan 18 '17 at 21:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/133495/discussion-between-kody-and-jack-blank). – Kody Jan 18 '17 at 21:41
  • watchexec -w src -e purs -- spago – windmaomao Oct 24 '20 at 13:30
71

Nodemon also has a more fine-grained approach for watching folders and files. Use nodemon.json to specify what files and the types of files to watch, as below in your case:

{
  "watch": ["server.js", "src/"],
  "ext": "js, css"
}

Having a nodemon.json is particularly useful when the number and types of watched files start to bloat, and also when you want to run a script upon each server restart. For nodemon to read in the configuration, nodemon.json should be placed at the root directory of your project, along with every other hidden and not hidden json files.

Below is a good place to start your nodemon.json.

https://github.com/remy/nodemon/blob/master/doc/sample-nodemon.md

Hank Chan
  • 1,706
  • 1
  • 16
  • 21
25

I use this for hot replacement, nodemon --watch src and run tsc complier.

you can also check this article: https://medium.com/netscape/start-building-web-apps-with-koajs-and-typescript-366264dec608

"scripts": {
  "watch-server": "nodemon --watch 'src/**/*' -e ts,tsx --exec 'ts-node' ./src/server.ts"
}
Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
Kenneth Wu
  • 251
  • 3
  • 2
1

This solution worked for me. In the first create a file name nodemon.json in the home directory of your project and then add this

 {
  "restartable": "rs",
  "ignore": [
    ".git",
    "node_modules/**/node_modules"
  ],
  "verbose": true,
  "execMap": {
    "js": "node --harmony"
  },
  "events": {
    "restart": "osascript -e 'display notification \"App restarted due to:\n'$FILENAME'\" with title \"nodemon\"'"
  },
  "watch": [
    "test/fixtures/",
    "test/samples/"
  ],
  "env": {
    "NODE_ENV": "development"
  },
  "ext": "js,json"
}

you can add your directory name in the "watch" option to be monitored by the nodemon for any changes and add your files type in the "ext" option

-2

Install it:

npm install npm-watch

"scripts":

"watch": "npm-watch"
  • 1
    The question was explicitly about `nodemon` so this answer does not help. Your answer also lacks an explanation how the `npm-watch` package would be used to achieve what @op wants to achieve. – Fred Nov 21 '22 at 19:56