ts-node ignores d.ts files while tsc successfully compiles the project

TypescriptTypescript TypingsTs Node

Typescript Problem Overview


Having compiled my TypeScript project successfully, I intended to run it in VS Code's debug mode using ts-node. Problem is, ts-node can't find d.ts files I created (while tsc has no problem with it).

Project structure is:

/
    conf/
    dist/
    src/
        types/
package.json
tsconfig.json

tsconfig.json relevant entries are:

{
    "compilerOptions": {
        "target": "es2017",
        "module": "commonjs",
        // "lib": [],
        "sourceMap": true,
        "outDir": "dist",
        "rootDir": "src",
        "moduleResolution": "node",
        "baseUrl": ".",
        "paths": {
            "*": [
                "node_modules/*",
                "src/types/*"
            ]
        },
        // "rootDirs": [],
        // "typeRoots": [],
        // "types": [],
    },
    "include": [
        "src/**/*"
    ]
}

The definition file ts-node can't find is src/types/global.d.ts:

import { App } from '../App';

declare global {
    namespace NodeJS {
        interface Global {
            app: App;
        }
    }
}

So, trying to run it with ts-node I see:

TSError: ⨯ Unable to compile TypeScript:
src/boot.ts(15,59): error TS2339: Property 'app' does not exist on type 'Global'.

How to resolve it globally? I've found that /// <reference path="./types/global.d.ts" /> does the trick but I'd have to repeat it in every file using global.app.

My TypeScript version is 3.0.1

Typescript Solutions


Solution 1 - Typescript

I was having a similar problem, but I could not add --files, because I run ts-node by registering the module through mocha (i.e. mocha -r ts-node/register ...).

I could solve it by adding a files and a ts-node section to tsconfig.json like this:

// tsconfig.json
{
  "ts-node": {
    "files": true
  },
  "files": [
    "src/index.ts",
    "src/global.d.ts"
  ],
  "compilerOptions":{
    //...
  }
}

Solution 2 - Typescript

Starting with ts-node in 7.0.0, does not Load files from tsconfig.json on startup. Instead, you should specificy --files like this

ts-node --files src/boot.ts

Solution 3 - Typescript

TLDR

Add "ts-node": { "files": true }, in tsconfig.json for ts-node-dev to work as expected

explanation:

I was using ts-node-dev in package.json like:

"scripts": {
    "build": "tsc",
    ...
    "dev": "ts-node-dev src/index.ts"
  },

npm run build was working fine but npm run dev was failing, My type definition files are in src/types/*.

It started working fine after I added the following in my tsconfig.json

{
  "ts-node": {  "files": true }, // add this
  "compilerOptions": {
     ...
  }
}

Solution 4 - Typescript

I spent way to much time on this issue tried almost everything like adding to typeRoots my typings folder, creating typing folder with structure typings/module/index.d.ts but nothing worked out so now I've figured it out now what the above answer meant

With a new version of ts-node I've changed for my project's scripts:

ts-node@6: ts-node src/index.ts
ts-node@7: ts-node --files src/index

So your script will be changed to something like below

"scripts": {
    "dev": "nodemon --exec ts-node --files src/index",
  }

With the above in action your compile time increase a lot but I couldn't spend more time on this so I'm sticking to the above.

You also might like to visit https://github.com/TypeStrong/ts-node#help-my-types-are-missing.

Solution 5 - Typescript

Here's How i fixed it. Add "nodemon --exec ts-node --files src/app.ts" to your dev script.

 "scripts": {
    "start": "node dist/app.js",
    "dev": "nodemon --exec ts-node --files src/app.ts",
    "build": "tsc -p",
    "test": "echo \"Error: no test specified\" && exit 1"
  },

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionForsetiView Question on Stackoverflow
Solution 1 - TypescriptdjlaukView Answer on Stackoverflow
Solution 2 - Typescript欧阳斌View Answer on Stackoverflow
Solution 3 - TypescriptSahith VibudhiView Answer on Stackoverflow
Solution 4 - TypescriptBlack MambaView Answer on Stackoverflow
Solution 5 - TypescriptBasanta KcView Answer on Stackoverflow