#Paths not working

1 messages · Page 1 of 1 (latest)

uncut locust
#

I am trying to set up short-hand paths in my project to work with both my rootDir and outDir, and it keeps giving me this error:

Error: Cannot find module '@models/Client'
Require stack:
- C:\Users\~\dist\test\index.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1082:15)
    at Module._load (node:internal/modules/cjs/loader:928:27)
    at Module.require (node:internal/modules/cjs/loader:1149:19)
    at require (node:internal/modules/helpers:121:18)
    at Object.<anonymous> (C:\Users\~\dist\test\index.js:3:18)
    at Module._compile (node:internal/modules/cjs/loader:1267:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1321:10)
    at Module.load (node:internal/modules/cjs/loader:1125:32)
    at Module._load (node:internal/modules/cjs/loader:965:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    'C:\\Users\\~\\dist\\test\\index.js'
  ]
}

This is my tsconfig:

{
  "compilerOptions": {
    "target": "esnext",
    "module": "nodenext",
    "rootDir": "src",
    "outDir": "dist",
    "moduleResolution": "nodenext",
    "baseUrl": ".",

    "paths": {
      "@models/*": [
        "./src/models/*",
        "./dist/models/*"
      ]
    },

    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true
  },

  "include": [
    "src"
  ],

  "exclude": [
    "node_modules",
    "dist"
  ]
}

Thank you for considering my issue.

fathom kelp
#

!*:paths%

maiden vergeBOT
#
tjjfvi#0
`!t6:paths-are-not-magic`:

The paths and baseUrl compiler options don't cause any remapping of imports paths, they only inform TS of existing mappings, which you'll have to setup with some other tool.

baseUrl is a pretty well-supported option (e.g. using the NODE_PATH environment variable with node or resolve.modules with webpack).
paths can be trickier to setup, (especially with node see this for node), and you may find it to not be worth the effort.