ts-node does not find source types

Viewed 141

I have the following tsconfig:

{
  "compilerOptions": {
    "sourceMap": true,
    "esModuleInterop": true,
    "allowJs": true,
    "noImplicitAny": true,
    "moduleResolution": "node",
    "lib": [
      "esnext",
      "DOM"
    ],
    "module": "es2022",
    "target": "es2017",
    "rootDir": "src",
    "baseUrl": ".",
    "paths": {
      "*": [
        "node_modules/*",
        "src/types/*"
      ]
    },
    "types": [
      "node",
      "jest"
    ],
    "typeRoots": [
      "node_modules/@types",
      "src/types"
    ],
    "outDir": "./built"
  },
  "ts-node": {
    "esm": true
  },
  "include": [
    "./src/**/*",
    "jest.config.js"
  ],
  "exclude": [
    "node_modules",
    "dist"
  ]
}

which specifies src/types as one of the type roots.

I have the following type file found at src/types/custom.d.ts:

declare module 'mongo-uri-tool';

which is needed because the mongo-uri-tool package does not have types. I find that this resolves problems with tsc and my IDE. However, when I run tsnode, it complains even though I'm manually specifying the tsconfig.json I've shown above:

 ❮❮❮ NODE_OPTIONS='--experimental-specifier-resolution=node' npx ts-node -P tsconfig.json src/index.ts
/Users/paymahn/code/tripvector/tripvector-mono/backend/node_modules/ts-node/src/index.ts:820
    return new TSError(diagnosticText, diagnosticCodes);
           ^
TSError: ⨯ Unable to compile TypeScript:
src/lib/mongodb.ts:2:22 - error TS7016: Could not find a declaration file for module 'mongo-uri-tool'. '/Users/paymahn/code/tripvector/tripvector-mono/backend/node_modules/mongo-uri-tool/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/mongo-uri-tool` if it exists or add a new declaration (.d.ts) file containing `declare module 'mongo-uri-tool';`

2 import mongoUri from 'mongo-uri-tool';
                       ~~~~~~~~~~~~~~~~

    at createTSError (/Users/paymahn/code/tripvector/tripvector-mono/backend/node_modules/ts-node/src/index.ts:820:12)
    at reportTSError (/Users/paymahn/code/tripvector/tripvector-mono/backend/node_modules/ts-node/src/index.ts:824:19)
    at getOutput (/Users/paymahn/code/tripvector/tripvector-mono/backend/node_modules/ts-node/src/index.ts:1014:36)
    at Object.compile (/Users/paymahn/code/tripvector/tripvector-mono/backend/node_modules/ts-node/src/index.ts:1322:43)
    at transformSource (/Users/paymahn/code/tripvector/tripvector-mono/backend/node_modules/ts-node/src/esm.ts:380:37)
    at load (/Users/paymahn/code/tripvector/tripvector-mono/backend/node_modules/ts-node/src/esm.ts:280:51)
    at async ESMLoader.load (node:internal/modules/esm/loader:363:20)
    at async ESMLoader.moduleProvider (node:internal/modules/esm/loader:282:11) {
  diagnosticCodes: [ 7016 ]
}

why doesn't ts-node detect the custom type file even though tsc and Webstorm do?

0 Answers
Related