bubenshchykov / ngrok

Expose your localhost to the web. Node wrapper for ngrok.
https://ngrok.com
2.33k stars 317 forks source link

Types not found when `moduleResolution: node16` is set for typescript #288

Closed sorgloomer closed 2 years ago

sorgloomer commented 2 years ago

Describe the bug

Getting

main.ts:1:24 - error TS7016: Could not find a declaration file for module 'ngrok'. '.../node_modules/ngrok/index.js' implicitly has an 'any' type.

when trying to import in a project with moduleResolution: node16.

To Reproduce

package.json

{
  "dependencies": {
    "ngrok": "4.3.1",
    "typescript": "4.7.4"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "ESNext",
    "moduleResolution": "Node16",
    "noImplicitAny": true
  }
}

main.ts

import * as ngrok from 'ngrok';

and then run:

tsc

Output I get:

$ tsc
main.ts:1:24 - error TS7016: Could not find a declaration file for module 'ngrok'. '.../node_modules/ngrok/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/ngrok` if it exists or add a new declaration (.d.ts) file containing `declare module 'ngrok';`

1 import * as ngrok from 'ngrok';
                         ~~~~~~~

Found 1 error in main.ts:1

Additional context

My intent in this example is to import ngrok using require, and this is what's happening, but typescript doesn't seem to pick up the .d.ts file.

Typescripts esm support is not without controversies. One of them is that they are more strict about the location of .d.ts files: https://github.com/microsoft/TypeScript/issues/49160 . The most straightforward solution seems to be renaming ngrok.d.ts to index.d.ts, but there might be better ways I am not aware of.