0

I'm writing an application and decided to shorten paths using aliases. However, with all my settings I can't get Node.js to see the paths.

This is my tsconfig.json:

{
 "compilerOptions": {
 "baseUrl": ".",
 "outDir": "./dist",
 "paths": {
 "@utils/*": ["src/utils/*"],
 "@controlers/*": ["src/controlers/*"],
 "@middlewares/*": ["src/middlewares/*"],
 "@routes/*": ["src/routes/*"],
 "@generated/*": ["/generated/*"]
 },
 "target": "es2024",
 "module": "nodenext",
 "moduleResolution": "nodenext",
 "resolveJsonModule": true,
 "sourceMap": false,
 "noImplicitAny": false,
 "removeComments": true,
 "allowSyntheticDefaultImports": true,
 "esModuleInterop": true
 },
 "include": ["src"]
}

This is my package.json:

"scripts": {
 "dev": "tsc-watch --onSuccess \"node -r tsconfig-paths/register ./dist/index.js\"",
 "test": "echo \"Error: no test specified\" && exit 1"
},
 "keywords": [],
 "author": "",
 "license": "ISC",
 "type": "module",
},
"devDependencies": {
 "nodemon": "^3.1.10",
 "ts-node": "^10.9.2",
 "tsc-watch": "^7.1.1",
 "tsconfig-paths": "^4.2.0",
 "typescript": "^5.8.3"
},
 "_moduleAliases": {
 "@utils": "dist/utils",
 "@controlers": "dist/controlers",
 "@middlewares": "dist/middlewares",
 "@routes": "dist/routes",
 "@generated": "dist/generated"
}

How my inports in main TS file look:

import authRoutes from "@routes/auth_routes.js";

Also I tried nodemon with that setting, but this doesn't work either:

{
 "watch": ["src"],
 "ext": "ts, json",
 "exec": "node --loader ts-node/esm -r tsconfig-paths/register --experimental-specifier-resolution=node ./src/index.ts"
}

Ideally, I would like to run an application with real-time tracking without compilation, although if it is possible only with compilation then I am not against it. Of course, the simplest option is not to use all these paths and then everything will work, but why simplify everything.

jonrsharpe
123k31 gold badges278 silver badges489 bronze badges
asked Jul 29, 2025 at 10:10
10
  • 1
    This is the problem, "module": "nodenext". -r works for cjs only. tsconfig-paths is not intended for native esm. ts-node which you're trying to use too is abandoned and has problems with native esm too. tsx is considered a modern replacement, it supports watch. Did you try it? Commented Jul 29, 2025 at 10:21
  • @EstusFlask - that is, 2 options: 1. try tsx. 2. change the module to another. As for changing the module, I get errors with prisma orm, so I chose this particular module. Can you explain what you mean about tsx? if you are about this "tsc --watch", then I was getting the same error as in the current version of the stack Commented Jul 29, 2025 at 10:41
  • 2
    I mean this npmjs.com/package/tsx , not "tsc" Commented Jul 29, 2025 at 10:53
  • 1
    it seems to work, thanks. However now I get a message every time I need to generate prisma, maybe you know the answer to this too? Error: "@prisma/client did not initialize yet. Please run "prisma generate" and try to import it again.'" Commented Jul 29, 2025 at 11:12
  • 1
    Glad it worked for you Commented Jul 29, 2025 at 12:47

1 Answer 1

0

Tsx node solve my problems with path, and work in live now! Link https://www.npmjs.com/package/tsx

answered Jul 29, 2025 at 12:41
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.