Running task on file change in npm scripts
Suiseki opened this issue · 4 comments
Hi,
I would like to make a task, that is launched while one of the files in watched directory changes.
I am using npm scripts
"scripts": { "app01": "node alerts/changes.js", "watch": "watch ./js/*.js npm run app01" },
and running it in command line:
npm run watch
but it ends with error: ERR! Failed at the try02@1.0.2 watch script 'watch ./js/*.js npm run app01'. What is the proper syntax?
In my package.json:
"watch": "watch 'npm run debug-no-inspect' ./src -d",
then run it with npm run watch
For you you'd want to make a different entry in package.json:
"alert-change": "node alerts/changes.js"
And then put that in the single quotes above.
But you may be better off just spawning a file watcher node process by itself.
Thank you for the answer. I still have a problem. I can't run commands within single quotes. Is it matter of operation system (Windows 10). I am also not sure what command is in syntax:
watch <command> [...directory] [OPTIONS]
is it a name of my other custom script in package.json file as i.e. build:js, sometest od myaction could be? Or there is some fixed set of commands i can only use like build, install, help?
Following your example I made mine:
"watch": "watch npm run myfile ./js",
but i get an error, that there is no file rike 'run' in my directory.
Try escaping the quotes? This might be better off on StackOverflow...
Windows requires the use of double quotes (not a watch thing, it's a windows thing). You'll need to escape them as well so they aren't ending the JSON value early:
{
"scripts": {
"watch": "watch \"npm run myfile\" ./js",
}
}