1
votes

The problem:

I am trying to avoid relative module paths by setting the baseUrl option in my tsconfig.json file. Unfortunately it always says it can't find the module and I don't know why it can't find the module. Am I missing something / Do I need to do something else besides setting the baseUrl to my src folder in the tsconfig?

enter image description here

Actually intellisense is showing me the module's folder ('utils') when I try to import it: enter image description here

My project structure:

root

  • dist
  • src
    • MyProject.ts
    • utils
      • IntHelper.ts
  • tsconfig.json

File: MyProject.ts

import { IntHelper } from 'utils/IntHelper';

File: IntHelper.ts

export module IntHelper {
  export const xy: string = 'Test';
  export function crossSum(int: number) {
    return int; // Nonsense - ofcourse.
  }
}

Tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es6",
    "noImplicitAny": true,
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist",
    "baseUrl": "./src",
    "paths": {
      "*": [
        "node_modules/*",
        "src/types/*"
      ]
    }
  },
  "include": [
    "src/**/*"
  ]
}
1
Why are you trying to avoid relative module paths? - Tim Hutchison
@TimHutchison For medium+ sized projects it quickly becomes quite annoying to import modules using relative paths. Also if you decide to restructure your project or move files you would need to update all the relative paths. - kentor

1 Answers

2
votes

your paths are incorrect relative to your baseUrl. try changing it to:

"baseUrl": ".",
"paths": {
  "*": ["src/*"]
}