dividab / tsconfig-paths

Load node modules according to tsconfig paths, in run-time or via API.
MIT License
1.82k stars 104 forks source link

Paths aren't resolved if baseUrl has another value than "." #190

Open viT-1 opened 2 years ago

viT-1 commented 2 years ago

tsconfig.json in project root folder:

{
    "compilerOptions": {
        "baseUrl": ".",
        "paths": {
            "~/*": ["./src/*"],
            "#common/*": ["./src/common/*"]
        }
    }
}

I'm using your package in node esm script When resolve path #common/greeter-h/greeter-h.css with tsconfig above, all is ok, path resolved But if I changed tsconfig to

{
    "compilerOptions": {
        "baseUrl": "./src",
        "paths": {
            "~/*": ["./*"],
            "#common/*": ["./common/*"]
        }
    }
}

path isn't resolved. :thinking: but others packages use new tsconfig ("baseUrl": "./src") correctly:

rottmann commented 2 years ago

Have a similar problem, but baseUrl did not change anything for me.

/my-project/node_modules/ts-node/dist-raw/node-esm-resolve-implementation.js:774
  throw new ERR_MODULE_NOT_FOUND(packageName, fileURLToPath(base));
        ^
CustomError: Cannot find package '~' imported from /my-project/example/dev/api.ts
    at packageResolve (/my-project/node_modules/ts-node/dist-raw/node-esm-resolve-implementation.js:774:9)
    at moduleResolve (/my-project/node_modules/ts-node/dist-raw/node-esm-resolve-implementation.js:815:18)
    at Object.defaultResolve (/my-project/node_modules/ts-node/dist-raw/node-esm-resolve-implementation.js:929:11)
    at /my-project/node_modules/ts-node/src/esm.ts:228:33
    at entrypointFallback (/my-project/node_modules/ts-node/src/esm.ts:179:34)
    at resolve (/my-project/node_modules/ts-node/src/esm.ts:227:12)
    at resolve (/my-project/node_modules/ts-node/src/child/child-loader.ts:19:39)
    at ESMLoader.resolve (node:internal/modules/esm/loader:530:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:251:18)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:79:40)

example/dev/api.ts

// ok, it resolves the types-files with ~
import type {
  Files
} from '~/types'

// causes the error
import {
  FileType
} from '~/enums.js' // with or without .js, no difference

// ok
import {
  FileType
} from '../../src/enums.js'

tsconfig.json

{
  "ts-node": {
    "esm": true,
    "files": true,
    "require": [
      "tsconfig-paths/register"
    ],
    "transpileOnly": true
  },
  "compilerOptions": {
    "moduleResolution": "node",
    "module": "esnext",
    "target": "esnext",
    "importsNotUsedAsValues": "error",
    "preserveValueImports": true,
    "isolatedModules": true,
    "sourceMap": true,
    "allowJs": true,
    "checkJs": true,
    "composite": false,
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "suppressImplicitAnyIndexErrors": true,
    "useDefineForClassFields": true,
    "outDir": "./dist",
    "baseUrl": "./src",
    "paths": {
      "~/*": [ "*" ]
    },
    "strict": true,
    "types": [
      "node"
    ]
  },
  "include": [
    "example/**/*.ts",
    "src/**/*.d.ts",
    "src/**/*.ts"
  ],
  "exclude": [
    "dist",
    "node_modules"
  ]
}