Ts-node-dev: Wrong tsconfig.json

Created on 20 Aug 2019  Â·  12Comments  Â·  Source: wclr/ts-node-dev

I know there were quite some issues regarding the tsconfig.json file. I'm getting compiling issues, because the file I'd like to compile is in a subfolder, that has its own tsconfig.json file.

TypeScript's tsc command has the -p flag, that lets me select my tsconfig.json file like so:
tsc -p ./server/tsconfig.json.

In order to do this for ts-node-dev I created the following command:
cd server && ts-node-dev --respawn --transpileOnly server.ts && cd ..

My guess is that this could be simplified.


My folder structure:

node_modules/
server/
│  ├── server.ts
│  └── tsconfig.json
package.json
webpack.config.js

Most helpful comment

Try ts-node-dev --project ./server/tsconfig.json ./server/server.ts

At least that works for me, looks like options have to be specified first. (Which - to be fair - it does say that in the docs. Script name and arguments have to be last.)

All 12 comments

see https://github.com/TypeStrong/ts-node options
there is --project flag

@whitecolor ts-node ./server/server.ts --project ./server/tsconfig.json does not seem to recognize the tsconfig.json file. I know this, because I'm getting the use the 'esModuleInterop' flag error, while I included this in my ./server/tsconfig.json.

I have the same problem with this. --project does not work

TBH I would expect it to just pick up the tsconfig in that folder automatically

Try ts-node-dev --project ./server/tsconfig.json ./server/server.ts

At least that works for me, looks like options have to be specified first. (Which - to be fair - it does say that in the docs. Script name and arguments have to be last.)

Issue

I do believe the --project appears to be working by itself like @s-h-a-d-o-w recommended. However, I believe I have found something else that involves the tsconfig.json and that command. I have a project that uses paths in the tsconfig and requires a module to be loaded to make those work in my app. Upon running the following command with ts-node-dev it throws the following exception contrary to running the same exact command with ts-node which evaluates just fine.

I have a tsconfig in the root of the project and then another in the server folder which contains the paths. The one in the server folder extends off the one in the root and is the one that should be getting used for everything.

Command that works:
ts-node --project src/server/tsconfig.json -r tsconfig-paths/register src/server/server.ts

Command that doesn't work:
ts-node-dev --project src/server/tsconfig.json -r tsconfig-paths/register src/server/server.ts

It seems as though maybe something is being run in the wrong order? The tsconfig-paths/register module doesn't appear to be getting the specified --project option applied to it and is looking at the wrong tsconfig.json.

Temporary Fix: To get it working for now I have to do what @DerkJanSpeelman recommended which was to cd into the directory first and then execute.
cd src/server && ts-node-dev -r tsconfig-paths/register server.ts

Error

> [email protected] start:dev C:\Users\MY_USER\Desktop\app
> ts-node-dev --project src/server/tsconfig.json -r tsconfig-paths/register src/server/server.ts

Using ts-node version 8.6.2, typescript version 3.7.5
Missing baseUrl in compilerOptions. tsconfig-paths will be skipped
Error: Cannot find module '@app'
    at Function.Module._resolveFilename (module.js:547:15)
    at Function.Module._load (module.js:474:25)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (C:\Users\MY_USER\Desktop\app\src\server\server.ts:1:1)
    at Module._compile (module.js:652:30)
    at Module._compile (C:\Users\MY_USER\Desktop\app\node_modules\source-map-support\source-map-support.js:541:25)
    at Module.m._compile (C:\Users\MY_USER\AppData\Local\Temp\ts-node-dev-hook-33964689396950587.js:57:25)
    at Module._extensions..js (module.js:663:10)
    at require.extensions.(anonymous function) (C:\Users\MY_USER\AppData\Local\Temp\ts-node-dev-hook-33964689396950587.js:59:14)
[ERROR] 08:10:40 Error: Cannot find module '@app'

I got it to pick up the correct tsconfig by setting TS_NODE_PROJECT=path/to/tsconfig.json ts-nod-dev ...

I just picked up an old project and am seeing this issue again. Downgrading from 1.0.0-pre.63 to 1.0.0-pre.43 seems to fix this

I am experiencing the same issue as @Jake13f suggested above,

I have a base tsconfig.json inside the project root. I have tsconfig.server.json & tsconfig.app.json that extends 'tsconfig,json'. However the issue is ts-node-dev doesn't pick up either of -p or --project flags. For, -p flag it things tsconfig.json is a binary, whereas for --project flag it omits it.

ts-node-dev --project ./tsconfig.server.json -r tsconfig-paths/register --respawn server/index --cls

gives

  [INFO] 09:18:31 ts-node-dev ver. 1.1.1 (using ts-node ver. 9.1.1, typescript ver. 4.1.3)
Missing baseUrl in compilerOptions. tsconfig-paths will be skipped
Error: Cannot find module '~/utils/response'
Require stack:
- /home/pavittarx/pavi/oneshot/server/routes/college.ts
- /home/pavittarx/pavi/oneshot/server/routes/index.ts
- /home/pavittarx/pavi/oneshot/server/index.ts
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
    at Function.Module._load (internal/modules/cjs/loader.js:667:27)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/home/pavittarx/pavi/oneshot/server/routes/college.ts:43:18)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Module._compile (/home/pavittarx/pavi/oneshot/node_modules/source-map-support/source-map-support.js:547:25)
    at Module.m._compile (/tmp/ts-node-dev-hook-9041978835726594.js:69:33)
    at Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at require.extensions.<computed> (/tmp/ts-node-dev-hook-9041978835726594.js:71:20)
[ERROR] 09:18:34 Error: Cannot find module '~/utils/response'
Require stack:
- /home/pavittarx/pavi/oneshot/server/routes/college.ts
- /home/pavittarx/pavi/oneshot/server/routes/index.ts
- /home/pavittarx/pavi/oneshot/server/index.ts
  $ ts-node-dev -p tsconfig.server.json -r tsconfig-paths/register --respawn server/index --cls

  [INFO] 09:27:15 ts-node-dev ver. 1.1.1 (using ts-node ver. 9.1.1, typescript ver. 4.1.3)
  Missing baseUrl in compilerOptions. tsconfig-paths will be skipped
  [eval]:1
  tsconfig.server.json
  ^

  ReferenceError: tsconfig is not defined
      at [eval]:1:1
      at Script.runInThisContext (vm.js:120:18)
      at Object.runInThisContext (vm.js:309:38)
      at Object.<anonymous> ([eval]-wrapper:10:26)
      at Module._compile (internal/modules/cjs/loader.js:999:30)
      at Module._compile (/home/pavittarx/pavi/oneshot/node_modules/source-map-support/source-map-support.js:547:25)
      at evalScript (internal/process/execution.js:94:25)
      at internal/main/eval_string.js:23:3

Any fix or workarounds as of yet?

Does it work with ts-node?

No, the issue presists with ts-node as well. I was experencing this when defining baseUrl & paths in extended tsconfig file. So, I have resorted to defining the path in base tsconfig file only. I don't encounter the issue any further this way. Also, vscode picks it up and doesn't show any import errors.

No, the issue presists with ts-node as well.

Then this could be addressed upstream.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jraoult picture jraoult  Â·  3Comments

gipcompany picture gipcompany  Â·  8Comments

pvtri96 picture pvtri96  Â·  10Comments

sveyret picture sveyret  Â·  4Comments

Chnapy picture Chnapy  Â·  6Comments