Socket
Book a DemoInstallSign in
Socket

koa-chokidar

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-chokidar

koa middleware to watch and reload files on change. Makes nodemon unnecessary.

1.0.2
latest
Source
npmnpm
Version published
Weekly downloads
2
100%
Maintainers
1
Weekly downloads
 
Created
Source

koa-chokidar

koa middleware to watch and reload files on change. Makes nodemon unnecessary.

NPM version gzip

build status

Why

Inspired by Don't use nodemon, there are better ways! and connect-chokidar, this module alleviates the pain of waiting for webpack (or your favorite bundler) to compile, anytime a hot-reloaded file changes on disk, for Koa apps.

Install

> npm i koa-chokidar

// or if using yarn

> yarn add koa-chokidar

Usage

const createWatcher = require('koa-chokidar')

const koaChokidar = createWatcher(`${__dirname}`, {
  // Unless specified explictly, `process.env.NODE_ENV !== 'production'` will be used as the default
  // so in production mode, this middleware will do nothing (to override, set it to `true`)
  watch: process.env.NODE_ENV !== 'production',

  // Only the filenames that are in the regexp below will be deleted
  // from `require.cache` when files change
  requireCacheToRemoveRe: /[/\\]src[/\\]/,

  // `chokidar` opts can be provided here
  chokidar: {
    ignored: ['node_modules']
  }
})

// ...

const router = require('koa-router')()

// You'll have to `require()` all of the files that your
// local middleware or router depend on. This is because if we required it on top,
// the router / middleware would always refer to that instance and there would be
// no way of clearing it.
router.get('/', koaChokidar(() => require('./src/homepage')))
router.get('/users', koaChokidar(() => require('./src/user-api')))

Example

Checkout the example folder for a sample koa app. You can run the example app by:

$ git clone https://github.com/mrchief/koa-chokidar
$ cd koa-chokidar/example
$ npm install
$ npm start

Visit http://localhost:8000 in any browser. Then try changing the response in any of the watched files and hit refresh on the browser. Watch the response change without restarting the node process!

The sample app uses koa-router but you can use any other router/middleware. The idea remains the same - load your watched files via koa-chokidar. Anything required/imported outside of koa-chokidar will not be watched/reloaded.

Debugging

Run your process with the NODE_DEBUG=koa-chokidar environment variable set.

License

License

Released 2018 by Hrusikesh Panda

Keywords

koa

FAQs

Package last updated on 19 Mar 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.