tsx: Watch not working with docker

Bug description

I have created one file named main.ts and am using tsx watch ./main.ts as my start script to watch the file. Locally everything is working fine (Node 20) but when I try to run in a docker container, it does not watch changes in main.ts. I have mounted my local files to container. Screenshot 2023-07-12 220303

I was expecting it to reload when I make changes in main.ts locally.

Nothing happens. tsx does not reload anything.

I did not get any error

Reproduction

This is my main.ts

console.log('Hello world!');

This is my Dockerfile (I have tried with node:alpine too)

FROM node:18-alpine
WORKDIR /app
COPY package.json .
COPY package-lock.json .
RUN npm install
COPY . .
CMD [ "npm", "run", "start"]

This is the docker-compose.yml

version: '3.8'
services:
  tsx-test:
    build: .
    volumes:
      - .:/app
      - nodeModules:/app/node_modules

volumes:
  nodeModules:

Environment

System:
    OS: Windows 10 10.0.22621
    CPU: (8) x64 Intel(R) Core(TM) i5-9300H CPU @ 2.40GHz
    Memory: 1.23 GB / 7.85 GB
  Binaries:
    Node: 20.1.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 3.5.1 - C:\Program Files\nodejs\yarn.CMD
    npm: 9.6.4 - C:\Program Files\nodejs\npm.CMD
    pnpm: 8.6.7 - C:\Program Files\nodejs\pnpm.CMD
  npmPackages:
    tsx: ^3.12.7 => 3.12.7 

BTW, I am using Windows 11 not 10

Can you work on a fix?

  • I’m interested in opening a pull request to address this issue.

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 9
  • Comments: 22 (5 by maintainers)

Most upvoted comments

@adam-beck Yes, I just tried compose watch and it is behaving weirdly. When I use:

  • experimental native --watch: changes are reflected only once
  • tsx watch: changes are reflected around 4 times and then stops
  • nodemon: works fine even without polling!
  • tsx with CHOKIDAR_USEPOLLING=true works fine as expected

Not sure why tsx watch stops working after some time. Here is my compose.yml in case anyone is interested.

# compose.yml

services:
  server:
    build:
      context: .
    ports:
      - 3000:3000
    environment:
      - CHOKIDAR_USEPOLLING=true
    develop:
      watch:
        - action: sync
          path: ./src
          target: /app/src
        - action: rebuild
          path: package.json
          target: /app

For anyone reading who just wants this to work, set environment variable CHOKIDAR_USEPOLLING=1

It’s already mentioned above but I wanted to confirm it’s a quick work around that works for most cases

I checked @parcel/watcher and it also does not work with Docker on windows.

In fact, no watcher can actually handle this. The problem is in Windows, Docker uses WSL2 which uses its own file system (ext4 for Ubuntu) instead of windows NTFS. So, when I change something locally in windows, change happens in NTFS but container running in ext4 never gets notified of the change because of this bug in WSL: [WSL2] File changes made by Windows apps on Windows filesystem don’t trigger notifications for Linux apps.

So, until that issue is fixed (which will definitely take a long time) we have a few options:

I think switching to @parcel/watcher is actually a better long term option as Chokidar is a bit less active now.

Hi @privatenumber, I found the problem!

It is mentioned in Chokidar documentation that we need to set usePolling to true to be able to watch files over a network.

I tested locally and it seems to work but the problem is setting it to true can cause performance issue.

Then I checked Nodemon to see how they have handled this and found that they use a flag --legacy-watch or -L to set usePolling to true.

Shall I add a similar flag to set usePolling in tsx?

I have a similar issue but I don’t know if it’s related. I’m using Compose Watch instead of volumes. For the first few file changes, it works great. But after like 4-5 it just stops. I’m not sure how to debug either.

In the meantime – and the reason I’m posting this comment – is that I resolved to using polling with chokidar. I’m still using tsx but I’m also passing in the environment variable CHOKIDAR_USEPOLLING=true to my container that is running tsx. Seems to “fix” the issue. Well, as good as any other alternative at the moment.

My host OS is PopOS and I’m using a version of Alpine Linux in the container. I’m using Docker Engine (not Docker for Linux).

Okay, lets add a --poll flag that accepts a interval value.