typescript-go icon indicating copy to clipboard operation
typescript-go copied to clipboard

Cannot find module '??' or its corresponding type declarations.

Open Gobd opened this issue 9 months ago • 9 comments

This happens when a dependency's package.json looks like this:

  "exports": {
    ".": {
      "node": {
        "types": "./dist/index.d.ts",
        "module": "./dist/index.module.js",
        "require": "./dist/index.js",
        "import": "./dist/index.mjs"
      },
      "browser": {
        "types": "./dist/index.d.ts",
        "import": "./dist/index.module.js",
        "require": "./dist/index.js"
      },
      "default": "./dist/index.module.js",
    },
    "./package.json": "./package.json"
  },

if you add "types": "./dist/index.d.ts" after the default the issue is fixed. This does not match regular typescript.

Gobd avatar Mar 12 '25 12:03 Gobd

We really need a fully cloneable repo in order to diagnose these, can you please provide one? Thanks!

RyanCavanaugh avatar Mar 12 '25 16:03 RyanCavanaugh

I think I ran into a similar issue. I created a minimal repo here: https://github.com/prochri/typescript-go-test

The imports work fine with JS tsc, but error on Go tsc:

src/file.ts:3:28 - error TS2307: Cannot find module 'three/examples/jsm/loaders/GLTFLoader' or its corresponding type declarations.

3 import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader"
                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

src/file.ts:4:24 - error TS2307: Cannot find module 'swiper' or its corresponding type declarations.

4 import { Swiper } from "swiper"
                         ~~~~~~~~

The exports field in package.json of threejs, for example, looks like this:

  "exports": {
    ".": {
      "import": "./build/three.module.js",
      "require": "./build/three.cjs"
    },
    "./examples/fonts/*": "./examples/fonts/*",
    "./examples/jsm/*": "./examples/jsm/*",
    "./addons": "./examples/jsm/Addons.js",
    "./addons/*": "./examples/jsm/*",
    "./src/*": "./src/*",
    "./webgpu": "./build/three.webgpu.js",
    "./tsl": "./build/three.tsl.js"
  },

prochri avatar Apr 10 '25 19:04 prochri

I ran into a similar issue with "@descope/nextjs-sdk" in a projecrt. TSGO is unable to resolve the imports field when the imported packages package.json export look like this:

"exports": {
  ".": {
    "import": {
      "types": "./dist/types/index.d.ts",
      "default": "./dist/esm/index.js"
    },
    "require": "./dist/cjs/index.js"
  },

However when modded to this it is able to resolve modules and types:

"exports": {
  ".": {
     "types": "./dist/types/index.d.ts",
     "default": "./dist/esm/index.js""
  },

EdwardIrby avatar Apr 15 '25 17:04 EdwardIrby

Same is happening in vite for me:

test.ts:3:10 - error TS2305: Module '"node_modules/.pnpm/[email protected]/node_modules/vite/index"' has no exported member 'searchForWorkspaceRoot'.
 import { searchForWorkspaceRoot } from "vite";

While in the editor the non tsgo version uses "node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/index" instead of '"node_modules/.pnpm/[email protected]/node_modules/vite/index"'.

Looking at the vite package.json:

{
  "name": "vite",
  "version": "6.3.5",
  "type": "module",
  "license": "MIT",
  "author": "Evan You",
  "description": "Native-ESM powered web dev build tool",
  "bin": {
    "vite": "bin/vite.js"
  },
  "keywords": [
    "frontend",
    "framework",
    "hmr",
    "dev-server",
    "build-tool",
    "vite"
  ],
  "main": "./dist/node/index.js",
  "types": "./dist/node/index.d.ts",
  "exports": {
    ".": {
      "module-sync": "./dist/node/index.js",
      "import": "./dist/node/index.js",
      "require": "./index.cjs"
    },
    "./client": {
      "types": "./client.d.ts"
    },
    "./module-runner": "./dist/node/module-runner.js",
    "./dist/client/*": "./dist/client/*",
    "./types/*": {
      "types": "./types/*"
    },
    "./types/internal/*": null,
    "./package.json": "./package.json"
  },
  "typesVersions": {
    "*": {
      "module-runner": [
        "dist/node/module-runner.d.ts"
      ]
    }
  },
  "imports": {
    "#module-sync-enabled": {
      "module-sync": "./misc/true.js",
      "default": "./misc/false.js"
    }
  },
  "files": [
    "bin",
    "dist",
    "misc/**/*.js",
    "client.d.ts",
    "index.cjs",
    "index.d.cts",
    "types"
  ],
  "engines": {
    "node": "^18.0.0 || ^20.0.0 || >=22.0.0"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/vitejs/vite.git",
    "directory": "packages/vite"
  },
  "bugs": {
    "url": "https://github.com/vitejs/vite/issues"
  },
  "homepage": "https://vite.dev",
  "funding": "https://github.com/vitejs/vite?sponsor=1",
  "//": "READ CONTRIBUTING.md to understand what to put under deps vs. devDeps!",
  "dependencies": {....},
  "optionalDependencies": {....},
  "devDependencies": {....},
  "peerDependencies": {....},
  "peerDependenciesMeta": {....},
  "scripts": {....}
}

I had to add "types": "./dist/node/index.d.ts", just AFTER "import": "./dist/node/index.js", and BEFORE "require": "./index.cjs" at exports["./"].

So the following snippet worked for me to remove the error (I didn't test building, just type checking):

  "types": "./dist/node/index.d.ts",
  "exports": {
    ".": {
      "module-sync": "./dist/node/index.js",
      "import": "./dist/node/index.js",
      "types": "./dist/node/index.d.ts", // ADDED THIS
      "require": "./index.cjs"
    },
    "./client": {
      "types": "./client.d.ts"
    },
    "./module-runner": "./dist/node/module-runner.js",
    "./dist/client/*": "./dist/client/*",
    "./types/*": {
      "types": "./types/*"
    },
    "./types/internal/*": null,
    "./package.json": "./package.json"
  },

jrmyio avatar May 08 '25 11:05 jrmyio

I found same error with rolldown-vite

Image

Work around: pnpm patch https://github.com/mizchi/play-rolldown-vite-tsgo/blob/main/patches/rolldown-vite.patch

mizchi avatar May 31 '25 07:05 mizchi

I had a similar issue when trying to import a file not defined in the package.json exports definition

https://github.com/MaintainX/yarn-plugin-workspace-lockfile/blob/8ae38c8799320b043f0db5cd1be3d81048711564/src/index.ts#L1-L14

// InstallOptions is not exported from `@yarnpkg/core` so just go get it from its definition
import { InstallOptions } from "@yarnpkg/core/lib/Project";

This works fine in normal typescript

Cellule avatar Jun 26 '25 16:06 Cellule

@mizchi's repro in https://github.com/microsoft/typescript-go/issues/518#issuecomment-2924630799 was the same as #904, now fixed.

In @prochri's repro in https://github.com/microsoft/typescript-go/issues/518#issuecomment-2794926185, things are behaving "as expected" because that repo is attempting to use the old/bad node10 module resolution mode. Flip the repro to nodenext, bundler, etc, and even TS 5.8 complains.

The same applies to @Cellule's repro above: https://github.com/microsoft/typescript-go/issues/518#issuecomment-3009126440

That's also using the old node10 resolution. Flipping it to nodenext shows the same exact problem in 5.8.

npx tsc
src/index.ts:12:32 - error TS2307: Cannot find module '@yarnpkg/core/lib/Project' or its corresponding type declarations.

12 import { InstallOptions } from "@yarnpkg/core/lib/Project";
                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~


Found 1 error in src/index.ts:12

So, these are all just down to "we are not bringing the very-not-recommended node10 resolution forward".

jakebailey avatar Jun 26 '25 21:06 jakebailey

followed the instructions here but met with a wall of failed imports

Image tsconfig.json

It's a nextJS app , but have same issue with NESTJS backend.. Using cursor { "compilerOptions": { "lib": [ "dom", "dom.iterable", "esnext" ], "allowJs": true, "skipLibCheck": true, "strict": true, "noEmit": true, "esModuleInterop": true, "module": "esnext", "moduleResolution": "bundler", "resolveJsonModule": true, "isolatedModules": true, "jsx": "react-jsx", "incremental": true, "plugins": [ { "name": "next" } ], "baseUrl": "src", "paths": { "@/*": [ "./*" ], "@components/*": [ "components/*" ], // Optional: Path aliases "@utils/*": [ "utils/*" ], "@playwright-tests/*": [ "../playwright-tests/*" ] }, "target": "ES2022" // Enhanced type checking options // "exactOptionalPropertyTypes": true, // "noUncheckedIndexedAccess": true, // "noImplicitOverride": true, // Performance optimizations //"verbatimModuleSyntax": true, // Latest features //"useDefineForClassFields": true, // "noUncheckedSideEffectImports": true }, "include": [ "next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts", ".next/dev/types/**/*.ts" ], "exclude": [ "node_modules" ] }

damiangreen avatar Dec 05 '25 02:12 damiangreen

You can see there are errors in tsconfig.json; baseUrl has been removed. See https://github.com/microsoft/TypeScript/issues/62508#issuecomment-3348649259.

jakebailey avatar Dec 05 '25 03:12 jakebailey