Search code examples
node.jstypescriptdockerstartupts-node-dev

ts-node-dev takes 30 seconds to add watchers


Whether I use ts-node or ts-node-dev, it takes about the same amount of time to start my app server, and it is driving me crazy. I have attempted to resolve this issue several times without success.

I am running a Node Typescript server with ts-node-dev in a Docker container.

Here's the problem (There's a significant amount of watchers that are added after this within the span of 1-2s): docker logs

As you can see, to put it simply, it takes its sweet time adding watchers. For whatever reason, it adds the first few watchers over several seconds, but then it pauses for a significant amount of time.

Do you know of a way to resolve this issue or at least improve the start-up speed?

  • The startup command: ts-node-dev --poll --transpile-only --debug -r tsconfig-paths/register server.ts
  • The ts-config:
{
  "compilerOptions": {
    "target": "ESNext",                                  /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
    "module": "CommonJS",                                /* Specify what module code is generated. */
    "moduleResolution": "node",
    "rootDir": "./",                                  /* Specify the root folder within your source files. */
    "baseUrl": ".",                                  /* Specify the base directory to resolve non-relative module names. */
    "paths": {
      "@/*": ["./*"]
    },                                      /* Specify a set of entries that re-map imports to additional lookup locations. */
    "forceConsistentCasingInFileNames": true,            /* Ensure that casing is correct in imports. */
    "strict": true,                                      /* Enable all strict type-checking options. */
    "skipLibCheck": true,                                 /* Skip type checking all .d.ts files. */
    "incremental": true, // only recompile changed code
    "outDir": "./dist", // output to one directory
  },
  "exclude": [
    "migrations",
    "node_modules",
    "./dist"
  ]
}
  • The docker-compose config:
server:
    container_name: server
    build:
      context: ./server
      target: ${BUILD_TARGET}
    env_file: ./.env
    ports:
      - 3000
    depends_on:
      - db
    volumes:
      - ./server:/app
    tty: true
    restart: on-failure

Solution

  • Shoutout to David Maze for mentioning the bind-mount performance.

    For context, the ./server directory has everything for the server. By everything, I mean node_modules included. I adjusted the volumes to be a bit more specific, and...

    First watcher: 20:38:17 server.ts added watcher

    To deploying: 20:38:22 Deploying...

    It still takes a few seconds, but this is peanuts compared to how it was before. This is well within workable startup performance now.