0

I have a project with directory "project_folder/src/..." in the project_folder contains my tsconfig.json file "project_folder/tsconfig.json", I wish to use the "@src/" for resolving modules but its not working .

Here is my tsconfig file

{
  "compilerOptions": {
    "baseUrl": "./src",
    "paths": {
      "@src/*": ["*"]
    },
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true
  },
  "include": ["src/**/*.ts"],
  "exclude": ["node_modules", "dist"]
}

This is how I am trying to import a file import authController from "@src/controllers/AuthController"; I am not getting any Eslint error in the code but I am getting on my terminal when I try running my code.

Uncaught Error: Cannot find module '@src/controllers/AuthController'
Require stack:
- /Users/Me/Desktop/project_folder/src/routes/AuthRoutes.ts
- /Users/Me/Desktop/project_folder/src/server.ts
- /Users/Me/Desktop/project_folder/src/index.ts

I don't understand what's wrong.

I have tried changing the import method to import authController from "@/controllers/AuthController"; and import authController from "@controllers/AuthController"; but these didn't work, rather it only gave import errors

  • How do you run your code? – ghybs Jul 28 '23 at 19:03
  • using nodemon actually. "dev": "export NODE_OPTIONS='--trace-deprecation --abort-on-uncaught-exception' && export NODE_ENV=development && nodemon --exec ts-node ./src/index.ts" – Scott Lexium Jul 28 '23 at 19:51
  • 1
    Does this answer your question? [Typescript paths not working in an Express project](https://stackoverflow.com/questions/58187115/typescript-paths-not-working-in-an-express-project) – ghybs Jul 29 '23 at 01:39
  • finally got it working with this command "dev": "export NODE_OPTIONS='--trace-deprecation --abort-on-uncaught-exception' && export NODE_ENV=development && nodemon --exec ts-node -r tsconfig-paths/register ./src/index.ts", but I am stuck with the issue of the compiled js output files having the @ imports which javascript doesnt know what the @ sign means – Scott Lexium Aug 07 '23 at 15:04
  • "_the compiled js output files having the @ imports which javascript doesnt know_": do you mean you also transpile and run your code with another mean than what you describe in your previous comments? If you need help for that, please make sure to describe it in your question body by [editing it](https://stackoverflow.com/posts/76789092/edit). – ghybs Aug 07 '23 at 18:15

0 Answers0