Skip to content

paths in tsconfig.base.json not working #910

Closed
@urielzen

Description

@urielzen

I am using an nx angular project and have the following path in the tsconfig.base.json

     "@libs/util/shared": ["libs/util/shared/src/index.ts"],

then in a ts file this

import {
  getDefaultConfirmationDialogConfig,
  getDefaultDialogConfig,
  IIsReloadNeeded
} from '@libs/util/shared';

which results in this error Cannot find module '@libs/util/shared' or its corresponding type declarations

but changing it to relative path works without issues.

import {
  getDefaultConfirmationDialogConfig,
  getDefaultDialogConfig,
  IIsReloadNeeded
} from '../../../../../../../../../../libs/util/shared/src';

I am using
"@typescript/native-preview": "^7.0.0-dev.20250522.2",

and the vscode extension

Identifier
typescriptteam.native-preview
Version
0.20250522.2

Activity

jakebailey

jakebailey commented on May 22, 2025

@jakebailey
Member

Are you using baseUrl to make this work? baseUrl is not supported; you'll want to remove it and write that path mapping target relative to the tsconfig that it's in instead, e.g.:

"@libs/util/shared": ["../../libs/util/shared/src/index.ts"],

Or similar, which has worked since TS 4.1.

mikerudge

mikerudge commented on May 22, 2025

@mikerudge

I am getting the same thing. using the vscode extension but tsgo seems ok with it.

{
    "compilerOptions": {
        "target": "es5",
        "downlevelIteration": true,
        "lib": ["dom", "dom.iterable", "esnext"],
        "allowJs": true,
        "skipLibCheck": true,
        "strict": true,
        "noEmit": true,
        "esModuleInterop": true,
        "module": "esnext",
        "moduleResolution": "bundler",
        "resolveJsonModule": true,
        "isolatedModules": true,
        "jsx": "preserve",
        "incremental": false,
        "plugins": [
            {
                "name": "next"
            }
        ],

        "paths": {
            "@/hs/audit/*": ["./app/(health-safety-audit)/*"],
             // lots of other paths
            "@/*": ["./*"]
        }
    },
    "include": [
        "next-env.d.ts",
        "**/*.ts",
        "**/*.tsx",
        ".next/types/**/*.ts",
        "trigger.config.ts",
        "tailwind.config.ts"
    ],
    "exclude": ["node_modules"]
}
jhorgan24

jhorgan24 commented on May 22, 2025

@jhorgan24

I'm having similar problems with remapping paths. Our config has
"paths": { "@/*": [ "./src/*" ] }

If I try to import import { prisma } from '@/db'; I get Cannot find module '@/db' or its corresponding type declarations.

I've tried adding an explicit path for @/db and I've tried without the @ symbol but I get the same result.

kbrilla

kbrilla commented on May 23, 2025

@kbrilla
  "@libs/util/shared": ["libs/util/shared/src/index.ts"],

This is how whole NX Monorepo works, so there will be a lot of people having this issue.

kbrilla

kbrilla commented on May 23, 2025

@kbrilla

Are you using baseUrl to make this work? baseUrl is not supported; you'll want to remove it and write that path mapping target relative to the tsconfig that it's in instead, e.g.:

"@libs/util/shared": ["../../libs/util/shared/src/index.ts"],

Or similar, which has worked since TS 4.1.

Tried this one, finally correct autocompletes so thats awesome! But still does not work with ts-go

michaelschufi

michaelschufi commented on May 23, 2025

@michaelschufi

I'm experiencing the same issue with the VS Code extension. I have not yet updated the typescript version inside the project, as that is not the goal right now.

We are using the following tsconfig file hierarchy.
Yes, we are using baseUrl, but with ${configDir} so I'm unsure if that's correct or not.

// base.json
{
  "$schema": "https://json.schemastore.org/tsconfig",
  "display": "Base-DO-NOT-USE-DIRECTLY",
  "compilerOptions": {
    /* Base Options: */
    "target": "es2022",

    "allowJs": true,
    "resolveJsonModule": true,
    "esModuleInterop": true,

    "skipLibCheck": true,

    "moduleDetection": "force",
    "isolatedModules": true,

    /* Strictness */
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "noImplicitOverride": true,

    /* Other */
    "incremental": true,
    "baseUrl": "${configDir}",
    "paths": {
      "@/*": ["${configDir}/src/*"],
      "react": ["${configDir}/node_modules/@types/react"]
    },
    "tsBuildInfoFile": "${configDir}/.cache/tsbuildinfo.json",
    "outDir": "${configDir}/dist"
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "**/*.js"],
  "exclude": ["**/node_modules", "node_modules"]
}
// next.json
{
  "$schema": "https://json.schemastore.org/tsconfig",
  "display": "Next.js",
  "extends": "./base.json",
  "compilerOptions": {
    "lib": ["ESNext", "dom", "dom.iterable"],
    "module": "ESNext",
    "moduleResolution": "bundler",
    "jsx": "preserve",
    "noEmit": true,
    "paths": {
      "@/*": ["./src/*"],
      "react": ["./node_modules/@types/react"]
    },
    "plugins": [
      {
        "name": "next"
      }
    ]
  },
  "include": [
    "${configDir}/next-env.d.ts",
    "${configDir}/.next/types/**/*.ts",
    "${configDir}/**/*.ts",
    "${configDir}/**/*.tsx",
    "${configDir}/**/*.mjs",
    "${configDir}/**/*.js"
  ],
  "exclude": ["node_modules"]
}

and finally the project's tsconfig.json

// tsconfig.json
{
  "extends": "tsconfig/nextjs",
  "compilerOptions": {
    "paths": {
      "@/*": ["./src/*"],
      "react": ["./node_modules/@types/react"]
    },
    "verbatimModuleSyntax": false
  },
  "include": [
    "./next-env.d.ts",
    "./.next/types/**/*.ts",
    "./**/*.ts",
    "./**/*.tsx",
    "./**/*.mjs",
    "./**/*.js",
    "./**/*.cjs",
    ".next/types/**/*.ts"
  ]
}

(I cannot at this time verify if the CLI is working, as there seems to be some different problem with that on my machine right now.)

mikerudge

mikerudge commented on May 24, 2025

@mikerudge

It looks like mine is due to me using () in my paths.
#932

eurmn

eurmn commented on May 25, 2025

@eurmn

It looks like mine is due to me using () in my paths. #932

Same here, paths are resolved in all files but those in which the path contains parenthesis.

PeterStaev

PeterStaev commented on May 28, 2025

@PeterStaev

I'm also having the problem and the paths do not have parenthesis around them. When I inspect the output of the TS server, I note that despite using relative URLs, it tries to resolve the paths always in the node_modules folder of the directory and its parent directories. It never tries to resolve it outside the node_modules folder.

jakebailey

jakebailey commented on Jun 4, 2025

@jakebailey
Member

Is this still happening for people as of the most recent nightly? The parens thing has been fixed, as have many other module resolution errors.

michaelschufi

michaelschufi commented on Jun 4, 2025

@michaelschufi

I can confirm that the path resolution errors with my config (#910 (comment)) are completely gone :D 🙏

There are still some other module resolution inconsistencies with the current non-native version, but those cases are not related to compilerOptions.paths - at least for me.

jakebailey

jakebailey commented on Jun 4, 2025

@jakebailey
Member

I'll close this, then. Please open new issues for anything else you're finding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Domain: Module ResolutionRelated to module resolution and module checker errors

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @michaelschufi@jakebailey@urielzen@mikerudge@PeterStaev

        Issue actions

          paths in tsconfig.base.json not working · Issue #910 · microsoft/typescript-go