I have been using path aliasing in my NextJS project for months now, but I recently changed to a custom server where I am using ts-node
to run my code, and my path aliasing just doesn’t work anymore.
I run ts-node server.ts
with this tsconfig:
{
"compilerOptions": {
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"typeRoots": [
"./types"
],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"module": "CommonJS",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"plugins": [
{
"name": "next"
}
],
"baseUrl": "./",
"paths": {
"@/*": [
"./*"
]
},
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts"
],
"exclude": [
"node_modules"
]
}
and get this error: Error: Cannot find module '@/lib/mock-db'
. Note, when I change the import to a relative path like ../../lib/mock-db
, it works fine.