paulmillr / chokidar

Minimal and efficient cross-platform file watching library
https://paulmillr.com
MIT License
10.8k stars 574 forks source link

Help!!! Chokidar watch not working #1311

Closed fewbadboy closed 4 months ago

fewbadboy commented 4 months ago

Hello, everyone! test code just like this:

const { appendFile } = require('node:fs/promises')
const Koa = require('koa')
const { join } = require('node:path')
const chokidar = require('chokidar')
const { watch } = require('node:fs')

const app = new Koa()
const watcher = chokidar.watch(join(process.cwd(), 'log/message.txt'), {
  ignored: /(^|[\/\\]\..)/,
  persistent: true,
  ignoreInitial: false,
  usePolling: true
})
watcher.on('ready', () => {
  console.log('Watcher is now ready.');
});

watcher.on('error', (error) => {
  console.error('Error happened while watching file:', error);
});

// can't work
watcher.on('all', (event, path) => {
  console.log(`${event} ${path}`)
})

// can work
watch(join(process.cwd(), 'log/message.txt'), (event, filename) => {
  console.log(`log: ${event} ${filename}`)
})

app.use(async(ctx, next) => {
  if (ctx.path !== '/favicon.ico') {
    await appendFile('./log/message.txt', Date().toLocaleString() + '\n', 'utf8')
    ctx.body = {
      name: "wang"
    }
    ctx.type = "application/json"
    ctx.state = 200
    ctx.message = "ok"
  }
})

app.listen(3000)

os: Windows 11 node: 20.11.0 chokidar: ^3.6.0