Skip to content

Instantly share code, notes, and snippets.

@k88hudson
Last active July 19, 2016 21:53
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k88hudson/ac77672339ad2e4d2552 to your computer and use it in GitHub Desktop.
Save k88hudson/ac77672339ad2e4d2552 to your computer and use it in GitHub Desktop.
how do i watch in npm scripts

This is an answer to https://twitter.com/tinkeraway/status/701746408072855552:

Calling npm run foo -- [something] just calls whatever is defined under the foo script and adds [something] to the end.

So if you have this in your scripts:

{
  "foo": "webpack"
}

Calling npm run foo will run webpack, and npm run foo -- -w will run webpack -w.

Npm does not implement any watch features itself, but many command line utilities (such as webpack, browserify, node-sass, etc.) have implemented watch you can trigger with a --watch/-w argument.

If you need to watch some files and run an arbitrary command when they change, you can use a package like chokidar-cli.

Here is an example:

{
  "foo": "foo ./blah",
  "watchfoo": "chokidar \"**/*.js\" -c \"npm run foo\""
}

Note that on windows, you should use (escaped) double quotes, but if you don't care about supporting windows, single quotes are fine.

@peterlindhard
Copy link

Brilliant. Too many new/unknown libs in at once, and nothing made sense when stuck. Thanks a bunch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment