cheeriojs / cheerio

The fast, flexible, and elegant library for parsing and manipulating HTML and XML.
https://cheerio.js.org
MIT License
28.69k stars 1.64k forks source link

Cannot find module '.' or its corresponding type declarations #2703

Closed poisoncat closed 2 years ago

poisoncat commented 2 years ago

when I use cheerio in my project with typescript, and try to compile it by tsc. The compiler throws an exception described below:

error TS2307: Cannot find module '.' or its corresponding type declarations.

2 import type { CheerioAPI, Cheerio } from '.'; 

Found 1 error in node_modules/cheerio/lib/esm/static.d.ts:2

This seems to be a bug in the package itself. How should I solve this problem? Thanks.

And I only use cheerio in my project like this:

import * as cheerio from "cheerio";
cheerio.load(content) // The type of content is string.

"cheerio": "^1.0.0-rc.12" "@types/cheerio": "^0.22.31" node v16.16.0 npm v8.16.0

javaarchive commented 2 years ago

The temporary workaround I came up with was to change '.' to './index.js' for the time being but this is a generated file so not sure how to fix in the source itself.

fb55 commented 2 years ago

Could you share the TypeScript version you are using?

poisoncat commented 2 years ago

Hi, the TypeScript version I'm using is the latest 4.7.4

javaarchive commented 2 years ago

Same version, tsconfig.json is set so that it should be producing js files with es6 imports.

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "Node16",
    "lib": ["ES2022"],
    "moduleResolution": "Node16",
    "rootDir": ".",
    "outDir": "build",
    "allowSyntheticDefaultImports": true,
    "importHelpers": true,
    "alwaysStrict": false,
    "sourceMap": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitReturns": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "noImplicitAny": false,
    "noImplicitThis": false,
    "strictNullChecks": false,
    "esModuleInterop": true,
    "allowJs": true
  },
  "include": ["src/**/*", "__tests__/**/*"]
}
poisoncat commented 2 years ago

My tsconfig.json file looks like this, but the compiler still throws the same error.

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "Node16",
    "moduleResolution": "node16",
    "rootDir": ".",
    "outDir": "build",
    "sourceMap": true,
    "allowSyntheticDefaultImports": true,
    "importHelpers": true,
    "alwaysStrict": false,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitReturns": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "noImplicitAny": false,
    "noImplicitThis": false,
    "strictNullChecks": false,
    "esModuleInterop": true,
    "allowJs": true
  },
  "exclude": [
    "node_modules"
  ],
  "include": [
    "./src/**/*", "__tests__/**/*"
  ]
}
fb55 commented 2 years ago

Thanks for sending this by — the import should be updated to conform with ESM. I will make sure this is covered in the next release.

manuth commented 2 years ago

As a workaround, I recommend you to add the following to your tsconfig.jsons compilerOptions:

{
    "compilerOptions": {
        "paths": {
            "cheerio": [
                "./node_modules/cheerio/lib/index.d.ts"
            ]
        }
    }
}

This will use cheerios cjs types instead of the esm types. The type declarations look exactly the same so it fits as a workaround replacement perfectly well.

wickning1 commented 2 years ago

I got this error when I changed moduleResolution from 'node' to 'node16' in my tsconfig.json. I had to do this for the 'file-type' library, so between a rock and a hard place.

@manuth's workaround helped, thanks.

andreash commented 1 year ago

This still occurs with rc-12. I needed to go back to rc-10, to resolve this. Any clues?

cdwmhcc commented 12 months ago

Any update?

cdwmhcc commented 12 months ago

As a workaround, I recommend you to add the following to your tsconfig.jsons compilerOptions:

{
    "compilerOptions": {
        "paths": {
            "cheerio": [
                "./node_modules/cheerio/lib/index.d.ts"
            ]
        }
    }
}

This will use cheerios cjs types instead of the esm types. The type declarations look exactly the same so it fits as a workaround replacement perfectly well.

When use tsx modules, error: ReferenceError: _default is not defined

virtuallyunknown commented 9 months ago

As a workaround, I recommend you to add the following to your tsconfig.jsons compilerOptions:

{
    "compilerOptions": {
        "paths": {
            "cheerio": [
                "./node_modules/cheerio/lib/index.d.ts"
            ]
        }
    }
}

This will use cheerios cjs types instead of the esm types. The type declarations look exactly the same so it fits as a workaround replacement perfectly well.

This worked great until I migrated my monorepository to use pnpm. Now it got really nasty.

{
    "compilerOptions": {
          "paths": {
            "cheerio": ["../../../node_modules/.pnpm/cheerio@1.0.0-rc.12/node_modules/cheerio/lib/index.d.ts"]
        }
    }
}
yincrash commented 7 months ago

It looks like the -rc12 release is from before the fix.

elalienx commented 3 months ago

It looks like the -rc12 release is from before the fix.

I can confirm. -rc12 does not contain the fix. You need to roll back to rc-10 as @andreash mentioned.