DEV Community

Md. Khalid Hossen
Md. Khalid Hossen

Posted on

Absoulate import in node express typescript project configrations.

If you want to enabled typescript express project for absoulate import and want remove ../.. from entire project this guide for you:

at first you need to configure tsconfig.json file where you need to configure absoulate path. You can check below file:


{
    "compilerOptions": {
      "target": "ES2020",
      "module": "commonjs",
      "strict": true,
      "esModuleInterop": true,
      "skipLibCheck": true,
      "forceConsistentCasingInFileNames": true,
      "outDir": "./dist",
      "rootDir": "./src",
      "baseUrl": ".",
      "paths": {
        "*": ["node_modules/*"],
        "@/*": ["src/*"], 
      }
    },
    "include": ["./env.d.ts", "src/**/*.ts"],
    "exclude": ["node_modules"]
  }

Enter fullscreen mode Exit fullscreen mode

Note:
here i have used path as @/* and src is my base url

Then you need to add another library for configure this, tsconfig-paths it and chage your running script then everything will be fine.

script should be like this:

"scripts": {
    "dev": "nodemon --watch src --exec ts-node --require tsconfig-paths/register --transpile-only src/main.ts",
  },
Enter fullscreen mode Exit fullscreen mode

Top comments (0)