npm-watch vs npm-run
NPM Task Runner Libraries Comparison
1 Year
npm-watchnpm-runSimilar Packages:
What's NPM Task Runner Libraries?

NPM task runner libraries are tools that help automate repetitive tasks in the development workflow, such as running scripts, watching files for changes, and executing commands based on specific triggers. They streamline the development process by allowing developers to define and manage tasks in a more organized manner, enhancing productivity and ensuring consistency across different environments. These libraries can significantly reduce the manual effort required for tasks like building, testing, and deploying applications, making them essential for modern web development.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
npm-watch167,01832514.6 kB199 months agoMIT
npm-run58,160186-77 years agoMIT
Feature Comparison: npm-watch vs npm-run

Task Execution

  • npm-watch:

    npm-watch specializes in executing tasks based on file changes. It continuously monitors specified files and triggers defined npm scripts whenever changes are detected, enhancing the development workflow by automating repetitive tasks.

  • npm-run:

    npm-run provides a simple interface to execute npm scripts defined in the package.json file. It allows for command chaining and can handle environment variables, making it versatile for various task execution scenarios.

Configuration Complexity

  • npm-watch:

    npm-watch requires a bit more configuration to set up the file watching behavior and specify the tasks to be executed. While this may add complexity, it provides powerful capabilities for automating tasks based on file changes.

  • npm-run:

    npm-run is designed to be user-friendly with minimal configuration required. It allows developers to quickly set up and execute tasks without extensive setup, making it accessible for beginners and efficient for experienced developers alike.

Use Cases

  • npm-watch:

    npm-watch is particularly beneficial in development environments where immediate feedback is crucial. It is commonly used for tasks like live reloading, compiling assets, and running tests automatically as developers make changes to the codebase.

  • npm-run:

    npm-run is best suited for projects that primarily need to execute scripts and manage dependencies without the need for file watching. It is ideal for running build scripts, tests, and other one-off commands in a straightforward manner.

Community and Support

  • npm-watch:

    npm-watch also has a supportive community, although it may not be as widely adopted as npm-run. However, it still offers sufficient documentation and examples to help developers implement file-watching functionality effectively.

  • npm-run:

    npm-run benefits from the extensive npm ecosystem, providing a wealth of community support and documentation. It is widely used and well-documented, making it easy for developers to find solutions and examples.

Performance

  • npm-watch:

    npm-watch is designed to be lightweight and responsive, ensuring that tasks are executed promptly upon file changes. However, performance can vary based on the number of files being watched and the complexity of the tasks being triggered.

  • npm-run:

    npm-run is efficient for executing scripts, but its performance largely depends on the complexity of the scripts being run. It is optimized for speed in executing npm commands and managing tasks without unnecessary overhead.

How to Choose: npm-watch vs npm-run
  • npm-watch:

    Choose npm-watch if your primary requirement is to monitor files for changes and automatically execute tasks in response. It is particularly useful for development environments where live reloading or immediate feedback on code changes is necessary, making it suitable for projects that involve frequent updates.

  • npm-run:

    Choose npm-run if you need a straightforward solution for executing npm scripts with additional features like environment variable management and command chaining. It is ideal for projects that require simple task execution without the need for extensive configuration or file watching capabilities.

README for npm-watch

npm-watch

Run scripts from package.json when files change.

Common Issues

  • monorepo setups: In a monorepo setup, npm-watch may fail with ENOENT. The solution is to have nodemon globally installed. See here for more context

Synopsis

Install it:

npm install npm-watch

Add a top-level "watch" config to your package.json and a "watch" script to your "scripts":

{
  "watch": {
    "test": "{src,test}/*.js"
  },
  "scripts": {
    "test": "tape test/*.js",
    "watch": "npm-watch"
  }
}

There is the possibility to watch for different tasks

  {
    "watch": {
      "run_android": {
        "patterns": [
          "app"
        ],
        "extensions": "ts,html,scss",
        "quiet": false
      },
      "run_ios": {
        "patterns": [
          "app"
        ],
        "extensions": "ts,html,scss",
        "quiet": false
      }
    },
    "scripts": {
      "watch_android": "npm-watch run_android",
      "watch_ios": "npm-watch run_ios",
      "run_android": "tns run android --emulator",
      "run_ios": "tns run ios --emulator"
    }
  }

The top level keys of the "watch" config should match the names of your "scripts", and the values should be a glob pattern or array of glob patterns to watch.

Also it is now possible to obtain a second parameter to define the script which should be run for watching and not watch all possible scripts at once.

If you need to watch files with extensions other than those that nodemon watches by default (.js, .coffee, .litcoffee), you can set the value to an object with patterns and extensions keys. You can also add an ignore key (a list or a string) to ignore specific files. Finally, you can add a quiet flag to hide the script name in any output on stdout or stderr, or you can use the inherit flag to preserve the original's process stdout or stderr. You can enable nodemon legacy watch and specify the restart delay in milliseconds with the corresponding flags.

The quiet flag was changed from a string to a boolean in 0.1.5. Backwards compatibility will be kept for two patch versions.

Use runOnChangeOnly to set the nodemon option --on-change-only. Setting this to true tells nodemon to execute script on change only, not startup.

{
  "watch": {
    "test": {
      "patterns": ["src", "test"],
      "extensions": "js,jsx",
      "ignore": "src/vendor/external.min.js",
      "quiet": true,
      "legacyWatch": true,
      "delay": 2500,
      "runOnChangeOnly": false
    }
  },
  "scripts": {
    "test": "tape test/*.js"
  }
}

Start the watcher with npm run watch in a terminal, then edit some files:

mkdir src test
npm run watch &
cat <<EOF > test/test-sum.js
var test = require('tape')
test('sum module', function (t) {
  var sum = require('../src/sum.js')
  t.ok(sum(1, 2), 3, "Sums appear correct")
  t.end()
})
EOF

(Feel free to use the editor of your choice, cat just makes for easy demos)

You should see that your tests ran automatically, and failed because src/sum.js is missing. Let's fix that:

cat <<EOF > src/sum.js
module.exports = function (a, b)  {
  return 1
}
EOF

Our tests will run again, and this time they almost work. Let's fix sum.js:

cat <<EOF > src/sum.js
module.exports = function (a, b)  {
  return a + b
}
EOF

Tests run perfectly, ship it to the enterprise!

Once you have the watcher running, you can force restart all tasks by entering rs. If you want to only force a single task, type the name of the key from the watch config (for example rs test).

Global Config

setMaxListeners

If too many listeners are added to an event emitter, node.js will send a warning (rightfully so) about potential memory leaks. The default maximum is 10. If you need more than that, you can add a top level global config to your package.json

"watchGlobalConfig": {
    "setMaxListeners": true
}

And max listeners will be set on the relevant processes to the minimum needed to avoid the warning.

Options

patterns

Array of paths to watch

"patterns": ["src", "test"]

extensions

Comma delimited list of file extensions to watch

"extensions": "js,jsx"

ignore

Add an ignore property to your watch object. The value of ignore can be a string if you only want to ignore a single glob:

"watch": {
  "build": {
    "ignore": "build",
    ...
  }
  ...
}

Or an array if you want to ignore multiple globs:

"watch": {
  "build": {
    "ignore": [
      "build",
      "node_modules"
    ],
    ...
  }
  ...
}

quiet

Boolean to hide the script name in any output on stdout and stderr

"quiet": false

inherit

Boolean to preserve the original process' stdout and stderr

"inherit": false

legacyWatch

Boolean to enable legacy watch

"legacyWatch": true

delay

Number of milliseconds to delay before checking for new files

"delay": 2500

clearBuffer

Boolean to clear the buffer after detecting a new change

"clearBuffer": true

verbose

Boolean to turn on the nodemons verbose mode

"verbose": true

silent

Boolean to turn on nodemons silent (quiet) mode Silent was used as we already had an existing flag called quiet. This may change in a future release

"silent": true

Acknowledgements

This module does very little but run nodemon for you, all credit for the reliable file watching and process restarting should go to there.

License

MIT