TS config path error. Error: Cannot find module '@/models/UserSchema'
Asked Answered
L

1

8

I am making a discord bot and I was trying the path aliases feature on typescript. but I somehow keep getting this error when I am using it. I am using NodeJs and using

Here is the error message

Error: Cannot find module '@/models/UserSchema'
Require stack:
- /mnt/c/Users/ASUS/Documents/_Frastio-Docs/dev/DinderBot/src/index.ts
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._resolveFilename (/mnt/c/Users/ASUS/Documents/_Frastio-Docs/dev/DinderBot/node_modules/module-alias/index.js:49:29)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/mnt/c/Users/ASUS/Documents/_Frastio-Docs/dev/DinderBot/src/index.ts:13:1)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Module.m._compile (/mnt/c/Users/ASUS/Documents/_Frastio-Docs/dev/DinderBot/node_modules/ts-node/src/index.ts:1056:23)

and here is the code when I am trying to use the path. It appears to be correct with no error on vscode but when I run the app its giving the cannot find module error

import Test from '@/models/TestSchema'
import User from '@/models/UserSchema'

enter image description here

My tsconfig.json

{
  "compilerOptions": {
    "strictNullChecks": true,
    "baseUrl": ".",
    "emitDecoratorMetadata": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "lib": ["es5", "es6"],
    "module": "CommonJS",
    "moduleResolution": "node",
    "outDir": "./dist",
    "paths": {
      "@/*": ["src/*"]
    },
    "resolveJsonModule": true,
    "sourceMap": true,
    "target": "es6",
    "types": ["node"]
  },
  "include": ["**/*.ts"],
  "exclude": ["node_modules"]
}

my folder structure, the tsconfig is on the outside of the src folder folder structure

Lavalava answered 29/12, 2021 at 4:15 Comment(0)
P
31

Looks like there's an issue with mapping paths using ts-node addressed on the official repo. They also provide a solution is to use tsconfig-paths to map so do the following steps:

  • Install tsconfig-paths:
npm i -D tsconfig-paths
  • Add following config into the tsconfig.json:
{
  "ts-node": {
    "require": ["tsconfig-paths/register"]
  }
}

// Or you can use it in your dev CLI without having to add above config like:
// `"dev": "ts-node -r tsconfig-paths/register path/to/index.ts",`
Pachydermatous answered 29/12, 2021 at 4:29 Comment(6)
@Pachydermatous could you pls share your ts.config.json? am getting tsconfig.json:14:5 - error TS5023: Unknown compiler option 'ts-node'Specter
@Specter You should put this line outside of compilerOptions.Schooner
so much happiness right now. thanks!Clericals
It didn't work for me :(Lambdoid
I get this error: Couldn't find tsconfig.json. tsconfig-paths will be skippedHeater
As suggested by tmhao2005, adding it like this in tsconfig.json would work { "compilerOptions": { // whatever you have here }, "ts-node": { "require": [ "tsconfig-paths/register" ] } }Godbey

© 2022 - 2024 — McMap. All rights reserved.