Layout von Websiten mit Bootstrap und Foundation
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. # Chokidar [![Weekly downloads](https://img.shields.io/npm/dw/chokidar.svg)](https://github.com/paulmillr/chokidar) [![Yearly downloads](https://img.shields.io/npm/dy/chokidar.svg)](https://github.com/paulmillr/chokidar)
  2. > A neat wrapper around Node.js fs.watch / fs.watchFile / FSEvents.
  3. [![NPM](https://nodei.co/npm/chokidar.png)](https://www.npmjs.com/package/chokidar)
  4. Version 3 is out! Check out our blog post about it: [Chokidar 3: How to save 32TB of traffic every week](https://paulmillr.com/posts/chokidar-3-save-32tb-of-traffic/)
  5. ## Why?
  6. Node.js `fs.watch`:
  7. * Doesn't report filenames on MacOS.
  8. * Doesn't report events at all when using editors like Sublime on MacOS.
  9. * Often reports events twice.
  10. * Emits most changes as `rename`.
  11. * Does not provide an easy way to recursively watch file trees.
  12. Node.js `fs.watchFile`:
  13. * Almost as bad at event handling.
  14. * Also does not provide any recursive watching.
  15. * Results in high CPU utilization.
  16. Chokidar resolves these problems.
  17. Initially made for **[Brunch](https://brunch.io/)** (an ultra-swift web app build tool), it is now used in
  18. [Microsoft's Visual Studio Code](https://github.com/microsoft/vscode),
  19. [gulp](https://github.com/gulpjs/gulp/),
  20. [karma](https://karma-runner.github.io/),
  21. [PM2](https://github.com/Unitech/PM2),
  22. [browserify](http://browserify.org/),
  23. [webpack](https://webpack.github.io/),
  24. [BrowserSync](https://www.browsersync.io/),
  25. and [many others](https://www.npmjs.com/browse/depended/chokidar).
  26. It has proven itself in production environments.
  27. ## How?
  28. Chokidar does still rely on the Node.js core `fs` module, but when using
  29. `fs.watch` and `fs.watchFile` for watching, it normalizes the events it
  30. receives, often checking for truth by getting file stats and/or dir contents.
  31. On MacOS, chokidar by default uses a native extension exposing the Darwin
  32. `FSEvents` API. This provides very efficient recursive watching compared with
  33. implementations like `kqueue` available on most \*nix platforms. Chokidar still
  34. does have to do some work to normalize the events received that way as well.
  35. On other platforms, the `fs.watch`-based implementation is the default, which
  36. avoids polling and keeps CPU usage down. Be advised that chokidar will initiate
  37. watchers recursively for everything within scope of the paths that have been
  38. specified, so be judicious about not wasting system resources by watching much
  39. more than needed.
  40. ## Getting started
  41. Install with npm:
  42. ```sh
  43. npm install chokidar
  44. ```
  45. Then `require` and use it in your code:
  46. ```javascript
  47. const chokidar = require('chokidar');
  48. // One-liner for current directory
  49. chokidar.watch('.').on('all', (event, path) => {
  50. console.log(event, path);
  51. });
  52. ```
  53. ## API
  54. ```javascript
  55. // Example of a more typical implementation structure:
  56. // Initialize watcher.
  57. const watcher = chokidar.watch('file, dir, glob, or array', {
  58. ignored: /(^|[\/\\])\../, // ignore dotfiles
  59. persistent: true
  60. });
  61. // Something to use when events are received.
  62. const log = console.log.bind(console);
  63. // Add event listeners.
  64. watcher
  65. .on('add', path => log(`File ${path} has been added`))
  66. .on('change', path => log(`File ${path} has been changed`))
  67. .on('unlink', path => log(`File ${path} has been removed`));
  68. // More possible events.
  69. watcher
  70. .on('addDir', path => log(`Directory ${path} has been added`))
  71. .on('unlinkDir', path => log(`Directory ${path} has been removed`))
  72. .on('error', error => log(`Watcher error: ${error}`))
  73. .on('ready', () => log('Initial scan complete. Ready for changes'))
  74. .on('raw', (event, path, details) => { // internal
  75. log('Raw event info:', event, path, details);
  76. });
  77. // 'add', 'addDir' and 'change' events also receive stat() results as second
  78. // argument when available: https://nodejs.org/api/fs.html#fs_class_fs_stats
  79. watcher.on('change', (path, stats) => {
  80. if (stats) console.log(`File ${path} changed size to ${stats.size}`);
  81. });
  82. // Watch new files.
  83. watcher.add('new-file');
  84. watcher.add(['new-file-2', 'new-file-3', '**/other-file*']);
  85. // Get list of actual paths being watched on the filesystem
  86. var watchedPaths = watcher.getWatched();
  87. // Un-watch some files.
  88. await watcher.unwatch('new-file*');
  89. // Stop watching.
  90. // The method is async!
  91. watcher.close().then(() => console.log('closed'));
  92. // Full list of options. See below for descriptions.
  93. // Do not use this example!
  94. chokidar.watch('file', {
  95. persistent: true,
  96. ignored: '*.txt',
  97. ignoreInitial: false,
  98. followSymlinks: true,
  99. cwd: '.',
  100. disableGlobbing: false,
  101. usePolling: false,
  102. interval: 100,
  103. binaryInterval: 300,
  104. alwaysStat: false,
  105. depth: 99,
  106. awaitWriteFinish: {
  107. stabilityThreshold: 2000,
  108. pollInterval: 100
  109. },
  110. ignorePermissionErrors: false,
  111. atomic: true // or a custom 'atomicity delay', in milliseconds (default 100)
  112. });
  113. ```
  114. `chokidar.watch(paths, [options])`
  115. * `paths` (string or array of strings). Paths to files, dirs to be watched
  116. recursively, or glob patterns.
  117. - Note: globs must not contain windows separators (`\`),
  118. because that's how they work by the standard —
  119. you'll need to replace them with forward slashes (`/`).
  120. - Note 2: for additional glob documentation, check out low-level
  121. library: [picomatch](https://github.com/micromatch/picomatch).
  122. * `options` (object) Options object as defined below:
  123. #### Persistence
  124. * `persistent` (default: `true`). Indicates whether the process
  125. should continue to run as long as files are being watched. If set to
  126. `false` when using `fsevents` to watch, no more events will be emitted
  127. after `ready`, even if the process continues to run.
  128. #### Path filtering
  129. * `ignored` ([anymatch](https://github.com/es128/anymatch)-compatible definition)
  130. Defines files/paths to be ignored. The whole relative or absolute path is
  131. tested, not just filename. If a function with two arguments is provided, it
  132. gets called twice per path - once with a single argument (the path), second
  133. time with two arguments (the path and the
  134. [`fs.Stats`](https://nodejs.org/api/fs.html#fs_class_fs_stats)
  135. object of that path).
  136. * `ignoreInitial` (default: `false`). If set to `false` then `add`/`addDir` events are also emitted for matching paths while
  137. instantiating the watching as chokidar discovers these file paths (before the `ready` event).
  138. * `followSymlinks` (default: `true`). When `false`, only the
  139. symlinks themselves will be watched for changes instead of following
  140. the link references and bubbling events through the link's path.
  141. * `cwd` (no default). The base directory from which watch `paths` are to be
  142. derived. Paths emitted with events will be relative to this.
  143. * `disableGlobbing` (default: `false`). If set to `true` then the strings passed to `.watch()` and `.add()` are treated as
  144. literal path names, even if they look like globs.
  145. #### Performance
  146. * `usePolling` (default: `false`).
  147. Whether to use fs.watchFile (backed by polling), or fs.watch. If polling
  148. leads to high CPU utilization, consider setting this to `false`. It is
  149. typically necessary to **set this to `true` to successfully watch files over
  150. a network**, and it may be necessary to successfully watch files in other
  151. non-standard situations. Setting to `true` explicitly on MacOS overrides the
  152. `useFsEvents` default. You may also set the CHOKIDAR_USEPOLLING env variable
  153. to true (1) or false (0) in order to override this option.
  154. * _Polling-specific settings_ (effective when `usePolling: true`)
  155. * `interval` (default: `100`). Interval of file system polling, in milliseconds. You may also
  156. set the CHOKIDAR_INTERVAL env variable to override this option.
  157. * `binaryInterval` (default: `300`). Interval of file system
  158. polling for binary files.
  159. ([see list of binary extensions](https://github.com/sindresorhus/binary-extensions/blob/master/binary-extensions.json))
  160. * `useFsEvents` (default: `true` on MacOS). Whether to use the
  161. `fsevents` watching interface if available. When set to `true` explicitly
  162. and `fsevents` is available this supercedes the `usePolling` setting. When
  163. set to `false` on MacOS, `usePolling: true` becomes the default.
  164. * `alwaysStat` (default: `false`). If relying upon the
  165. [`fs.Stats`](https://nodejs.org/api/fs.html#fs_class_fs_stats)
  166. object that may get passed with `add`, `addDir`, and `change` events, set
  167. this to `true` to ensure it is provided even in cases where it wasn't
  168. already available from the underlying watch events.
  169. * `depth` (default: `undefined`). If set, limits how many levels of
  170. subdirectories will be traversed.
  171. * `awaitWriteFinish` (default: `false`).
  172. By default, the `add` event will fire when a file first appears on disk, before
  173. the entire file has been written. Furthermore, in some cases some `change`
  174. events will be emitted while the file is being written. In some cases,
  175. especially when watching for large files there will be a need to wait for the
  176. write operation to finish before responding to a file creation or modification.
  177. Setting `awaitWriteFinish` to `true` (or a truthy value) will poll file size,
  178. holding its `add` and `change` events until the size does not change for a
  179. configurable amount of time. The appropriate duration setting is heavily
  180. dependent on the OS and hardware. For accurate detection this parameter should
  181. be relatively high, making file watching much less responsive.
  182. Use with caution.
  183. * *`options.awaitWriteFinish` can be set to an object in order to adjust
  184. timing params:*
  185. * `awaitWriteFinish.stabilityThreshold` (default: 2000). Amount of time in
  186. milliseconds for a file size to remain constant before emitting its event.
  187. * `awaitWriteFinish.pollInterval` (default: 100). File size polling interval, in milliseconds.
  188. #### Errors
  189. * `ignorePermissionErrors` (default: `false`). Indicates whether to watch files
  190. that don't have read permissions if possible. If watching fails due to `EPERM`
  191. or `EACCES` with this set to `true`, the errors will be suppressed silently.
  192. * `atomic` (default: `true` if `useFsEvents` and `usePolling` are `false`).
  193. Automatically filters out artifacts that occur when using editors that use
  194. "atomic writes" instead of writing directly to the source file. If a file is
  195. re-added within 100 ms of being deleted, Chokidar emits a `change` event
  196. rather than `unlink` then `add`. If the default of 100 ms does not work well
  197. for you, you can override it by setting `atomic` to a custom value, in
  198. milliseconds.
  199. ### Methods & Events
  200. `chokidar.watch()` produces an instance of `FSWatcher`. Methods of `FSWatcher`:
  201. * `.add(path / paths)`: Add files, directories, or glob patterns for tracking.
  202. Takes an array of strings or just one string.
  203. * `.on(event, callback)`: Listen for an FS event.
  204. Available events: `add`, `addDir`, `change`, `unlink`, `unlinkDir`, `ready`,
  205. `raw`, `error`.
  206. Additionally `all` is available which gets emitted with the underlying event
  207. name and path for every event other than `ready`, `raw`, and `error`. `raw` is internal, use it carefully.
  208. * `.unwatch(path / paths)`: **async** Stop watching files, directories, or glob patterns.
  209. Takes an array of strings or just one string. Use with `await` to ensure bugs don't happen.
  210. * `.close()`: Removes all listeners from watched files. Asynchronous, returns Promise.
  211. * `.getWatched()`: Returns an object representing all the paths on the file
  212. system being watched by this `FSWatcher` instance. The object's keys are all the
  213. directories (using absolute paths unless the `cwd` option was used), and the
  214. values are arrays of the names of the items contained in each directory.
  215. ## CLI
  216. If you need a CLI interface for your file watching, check out
  217. [chokidar-cli](https://github.com/kimmobrunfeldt/chokidar-cli), allowing you to
  218. execute a command on each change, or get a stdio stream of change events.
  219. ## Install Troubleshooting
  220. * `npm WARN optional dep failed, continuing fsevents@n.n.n`
  221. * This message is normal part of how `npm` handles optional dependencies and is
  222. not indicative of a problem. Even if accompanied by other related error messages,
  223. Chokidar should function properly.
  224. * `TypeError: fsevents is not a constructor`
  225. * Update chokidar by doing `rm -rf node_modules package-lock.json yarn.lock && npm install`, or update your dependency that uses chokidar.
  226. * Chokidar is producing `ENOSP` error on Linux, like this:
  227. * `bash: cannot set terminal process group (-1): Inappropriate ioctl for device bash: no job control in this shell`
  228. `Error: watch /home/ ENOSPC`
  229. * This means Chokidar ran out of file handles and you'll need to increase their count by executing the following command in Terminal:
  230. `echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p`
  231. ## Changelog
  232. For more detailed changelog, see [`full_changelog.md`](.github/full_changelog.md).
  233. - **v3.4 (Apr 26, 2020):** Support for directory-based symlinks. Macos file replacement fixes.
  234. - **v3.3 (Nov 2, 2019):** `FSWatcher#close()` method became async. That fixes IO race conditions related to close method.
  235. - **v3.2 (Oct 1, 2019):** Improve Linux RAM usage by 50%. Race condition fixes. Windows glob fixes. Improve stability by using tight range of dependency versions.
  236. - **v3.1 (Sep 16, 2019):** dotfiles are no longer filtered out by default. Use `ignored` option if needed. Improve initial Linux scan time by 50%.
  237. - **v3 (Apr 30, 2019):** massive CPU & RAM consumption improvements; reduces deps / package size by a factor of 17x and bumps Node.js requirement to v8.16 and higher.
  238. - **v2 (Dec 29, 2017):** Globs are now posix-style-only; without windows support. Tons of bugfixes.
  239. - **v1 (Apr 7, 2015):** Glob support, symlink support, tons of bugfixes. Node 0.8+ is supported
  240. - **v0.1 (Apr 20, 2012):** Initial release, extracted from [Brunch](https://github.com/brunch/brunch/blob/9847a065aea300da99bd0753f90354cde9de1261/src/helpers.coffee#L66)
  241. ## License
  242. MIT (c) Paul Miller (<https://paulmillr.com>), see [LICENSE](LICENSE) file.