Search code examples
typescriptvisual-studio-codenodemonts-node

Passing --project to ts-node when it's registered with nodemon


This command is used to run my server:1

nodemon
  -r ts-node/register
  -r tsconfig-paths/register
  --watch server/
  --watch shared/
  --ext ts,tsx,json
  --inspect-brk=24170
  ./server/index.ts

The problem is, I need ts-node to receive --project ./tsconfig-server.json rather than using the default tsconfig.json. Simply including it after the -r ts-node/register (or anywhere else) results in a bad option: --project error.

Using --exec 'node ./node_modules/ts-node/dist/bin.js --project ./tsconfig-amd.json' instead of -r ts-node/register works, but then the debugger fails to attach to the process. I assume that’s because it’s trying to attach to the nodemon process rather than the node process that nodemon starts?

But I cannot use the default tsconfig.json filename here, because VS Code does not support specifying an alternative tsconfig.json and the server config is not the one I want to use for viewing and editing the code


  1. The command is actually produced by VS Code per my .vscode/launch.json which looks like this:
    {
        // Use IntelliSense to learn about possible attributes.
        // Hover to view descriptions of existing attributes.
        // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
        "version": "0.2.0",
        "configurations": [
            {
                "type": "node",
                "request": "launch",
                "name": "nodemon",
                "runtimeExecutable": "node",
                "args": [
                    "${workspaceFolder}/server/index.ts"
                ],
                "runtimeArgs": [
                    "${workspaceFolder}/node_modules/nodemon/bin/nodemon.js",
                    "-r",
                    "ts-node/register",
                    "-r",
                    "tsconfig-paths/register",
                    "--watch",
                    "server/",
                    "--watch",
                    "shared/",
                    "--ext",
                    "ts,tsx,json"
                ],
                "restart": true,
                "console": "integratedTerminal",
                "internalConsoleOptions": "neverOpen",
                "protocol": "inspector"
            }
        ]
    }
    

Solution

  • You need to pass the --project value trough the "TS_NODE_PROJECT" environment variable. I do it in my nodemon.json file

    {
        "restartable": "rs",
        "ignore": [".git", "node_modules/**/node_modules", "src/client"],
        "verbose": true,
        "execMap": {
          "ts": "node --require ts-node/register/transpile-only --require tsconfig-paths/register"
        },
        "watch": ["src/server", "typing"],
        "env": {
          "NODE_ENV": "development",
          "TS_NODE_PROJECT": "src/server/tsconfig.json"
        },
        "ext": "js,json,ts"
    }