Absoulate import in node express typescript project configrations.



This content originally appeared on DEV Community and was authored by Md. Khalid Hossen

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"]
  }

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",
  },


This content originally appeared on DEV Community and was authored by Md. Khalid Hossen