node-dev-delay

Restarts your app when files are modified; has delay support.

node-dev (1)

Node-dev is a development tool for Node.jsthat automatically restarts the node process when a file is modified.

In contrast to tools like supervisoror nodemonit doesn't scan the filesystem for files to be watched. Instead it hooks into Node's require()function to watch only the files that have been actually required.

This means that you don't have to configure any include- or exclude rules. If you modify a JS file that is solely used on the client-side but never run on the server, node-dev will knowthis and won't restart the process.

This also means that you don't have toconfigure any file extensions. Just require a .jsonfile or a .coffeescript for example and it will be watched. Automatically.

Node-dev uses filewatcherunder the hood and hence will take advantage of the native fs.watch()API if it is available on your system.

Usage

Just run node-devas you would normally run node:

node-dev foo.js

There are a couple of command line options that can be used to control which files are watched and what happens when they change:

  • --no-depsWatch only the project's own files and linked modules (via npm link)
  • --all-depsWatch the whole dependency tree
  • --respawnKeep watching for changes after the script has exited
  • --dedupeDedupe dynamically
  • --graceful_ipc <msg>Send 'msg' as an IPC message instead of SIGTERM for restart/shutdown
  • --delay=<seconds>Delay restarting by seconds to allow multiple changes to queue.
  • --pollForce polling for file changes (Caution! CPU-heavy!)
  • --no-notifySwitch off desktop notifications (see below)

By default node-dev will watch all first-level dependencies, i.e. the ones in the project's node_modulesfolder.

Installation

Node-dev can be installed via npm. Make sure to use the -goption to install it globally.

npm install -g node-dev

Desktop Notifications

Status and error messages can be displayed as desktop notification using node-notifier:

Requirements:

  • Mac OS X: >= 10.8 or Growl if earlier.
  • Linux: notify-osd installed (Ubuntu should have this by default)
  • Windows: >= 8, task bar balloon if earlier or Growl if that is installed.
  • General Fallback: Growl
Settings

Usually node-dev doesn't require any configuration at all, but there are some options you can set to tweak its behaviour:

  • clear– Whether to clear the screen upon restarts. Default:false
  • notify– Whether to display desktop notifications. Default:true
  • timestamp– The timestamp format to use for logging restarts. Default:"HH:MM:ss"
  • vm– Whether to watch files loaded via Node's VMmodule. Default:true
  • fork– Whether to hook into child_process.fork(required for clusteredprograms). Default:true
  • deps– How many levels of dependencies should be watched. Default:1
  • dedupe– Whether modules should by dynamically deduped. Default:false
  • graceful_ipc- Send the argument provided as an IPC message instead of SIGTERM during restart events. Default:""(off)
  • delay- Delays the restarting of the script for the configured amount of seconds. Default:0

Upon startup node-dev looks for a .node-dev.jsonfile in the following directories:

  • user's HOME directory
  • the current working directory (as provided by process.cwd())
  • the same directory as the script to be run

Settings found later in the list will overwrite previous options.

Dedupe linked modules

Sometimes you need to make sure that multiple modules get exactly the same instanceof a common (peer-) dependency. This can usually be achieved by running npm dedupe– however this doesn't work when you try to npm linka dependency (which is quite common during development). Therefore node-dev provides a --dedupeswitch that will inject the dynamic-dedupemodule into your app.

Transpilers

You can also use node-dev to run transpiled languages. You can either use a .js file as entry point to your application that registers your transpiler as require-extension manually, for example by calling CoffeeScript.register()or you can let node-dev do this for you.

There is a config option called extensionswhich maps file extensions to compiler module names. By default this map looks like this:

{
        "coffee": "coffee-script/register",
        "ls": "LiveScript"
    }

This means that if you run node-dev foo.coffeenode-dev will do a require("coffee-script/register")before running your script.

Note:If you want to use coffee-script < 1.7 you have to change the setting to {"coffee": "coffee-script"}.

Options can be passed to a transpiler by providing an object containing nameand optionsattributes:

{
        "js": {
            "name": "babel-core/register",
            "options": {
                "only": [
                    "lib/**",
                    "node_modules/es2015-only-module/**"
                ]
            }
        }
    }

Graceful restarts

Node-dev sends a SIGTERMsignal to the child-process if a restart is required. If your app is not listening for these signals process.exit(0)will be called immediately. If a listener is registered, node-dev assumes that your app will exit on its own once it is ready.

Windows does not handle POSIX signals, as such signals such as SIGTERMcause the process manager to unconditionally terminate the application with no chance of cleanup. In this case, the option graceful_ipcmay be used. If this option is defined, the argument provided to the option will be sent as an IPC message via child.send("<graceful_ipc argument>"). The child process can listen and handle this event with:

process.on("message", function (msg) {
  if (msg === "<graceful_ipc argument") {
    // Gracefully shut down here
    doGracefulShutdown();
  }
});

Restart Delay

For the instances where a transpiler might make changes the multiple files in rapid succession, a delay can be configured. If any new changes are detected during the delay, the delay will be reset. The restart will occur when no changes happen during cfg.delayseconds.

Ignore paths

If you’d like to ignore certain paths or files from triggering a restart simply list them in the .node-dev.jsonconfiguration under "ignore", e.g.

{
  "ignore": [
    "client/scripts",
    "shared/module.js"
  ]
}

This might be useful when you are running a universal(isomorphic) web app that shares modules across the server and client, e.g. React.jscomponents for server-side rendering, which you don’t want to trigger a server restart when changed, since it introduces an unnecessary delay.

License

MIT

Repository

http://github.com/fgnass/node-dev.git


上一篇:@pollyjs/adapter-node-http
下一篇:nexus-cli-tmp

相关推荐

官方社区

扫码加入 JavaScript 社区