Ignore "cannot find module" error on typescript

29,191

Solution 1

If you just want to bypass the compiler, you can create a .d.ts file for that module, for instance, you could create a sql.d.ts file and inside have this:

declare module "sql" {
  let _sql: any;
  export = _sql;
}

Solution 2

As of TypeScript 2.6 (released on Oct 31, 2017), now there is a way to ignore all errors from a specific line using // @ts-ignore comments before the target line.

The mendtioned documentation is succinct enough, but to recap:

// @ts-ignore
const s : string = false

disables error reporting for this line.

However, this should only be used as a last resort when fixing the error or using hacks like (x as any) is much more trouble than losing all type checking for a line.

As for specifying certain errors, the current (mid-2018) state is discussed here, in Design Meeting Notes (2/16/2018) and further comments, which is basically

"no conclusion yet"

and strong opposition to introducing this fine tuning.

Solution 3

Solved This By default @ts-check looks for definitions of a module, either its your own code or external libraries.

Since we are not using ES6 style modules, then we must we are using commonjs, check my jsconfig.json file for help.

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "lib": ["es5", "es6", "es7"]
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules"],
  "typeAcquisition": { "enable": true }
}
Share:
29,191
Rafael
Author by

Rafael

Updated on June 30, 2020

Comments

  • Rafael
    Rafael almost 4 years

    Can the Typescript compiler ignore the cannot find module 'x' error on import expressions such as:

    //How to tell the compiler that this module does exists
    import sql = require('sql');
    

    There are multiple npm libraries such as node sql that doesn't have existing typings

    Is there a way to tell the compiler to ignore this error other than creating a new definition file with the declare module x ... ?

  • daslicht
    daslicht over 7 years
    Hi, how would the declaration look like for this module please: github.com/christophergregory/shopify-node-api/blob/master/l‌​ib/… I tried declaring it but I get Invalid module name in augmentation, module 'shopify-node-api' cannot be found.
  • Aron
    Aron about 7 years
    Is there no other way of getting round these errors without creating a separate dummy definition file for every module?
  • Aron
    Aron about 7 years
    Also that just didn't work. I got this error: Invalid module name in augmentation. Module 'express' resolves to an untyped module at '.../node_modules/express/index.js', which cannot be augmented
  • bluenote10
    bluenote10 over 3 years
    How does this approach work if the import is a @foo/bar import?