1

I have read through a bunch of posts and documentation but cant seem to get my TS Node Express Server to use absolute paths for imports.

Here is my project structure - src is the Typescript code and dist is the output directory.

.
├── dist
├── Dockerfile
├── node_modules
├── package.json
├── package-lock.json
├── public
├── readme.md
├── src
├── tsconfig.json
└── @types

Also here is my current tsconfig.json file:

{
  "compilerOptions": {
    "target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
    "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
    "outDir": "./dist" /* Redirect output structure to the directory. */,
    "strict": true /* Enable all strict type-checking options. */,
    "baseUrl": "./src",                             /* Base directory to resolve non-absolute module names. */
    "paths": { "./src/*": ["./src/*"]},                                 /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
    "typeRoots": ["@types", "@types/custom.d.ts"],                             /* List of folders to include type definitions from. */
    "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
    "skipLibCheck": true /* Skip type checking of declaration files. */,
    "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
  },
  "include": [
    "@types/custom.d.ts",
    "src"
  ],
  "exclude": ["node_modules"]
}
Nick McLean
  • 601
  • 1
  • 9
  • 23
  • Dear god, why would you be using absolute paths? I can't think of a more basic violation to a source code repository than that starting point. – Brian Ogden Apr 14 '21 at 02:47
  • Hmm - I just was hoping for simpler module imports. - avoiding `../../../routes/index` kind of sutff - Maybe this is not such a good thing here? Can you explain more please, why is this a violation - I am happy to change back to relative imports if it is better. Thanks for your input and thought! – Nick McLean Apr 14 '21 at 02:50
  • Check this out https://stackoverflow.com/questions/63744943/absolute-path-in-the-tsconfig-dose-not-work – Francesco Lisandro Apr 14 '21 at 02:57
  • Because as soon as your source code is worked on, on another machine, the absolute paths will be different and thus broken, ALWAYS USE RELATIVE PATHS or packages everywhere – Brian Ogden Apr 14 '21 at 16:45
  • A path like this `@app/controllers/main` if you could get it to work is fine, I am saying an absolute path like this: `/Users/joe.smith/Developer/source-code/file` <- Don't do that – Brian Ogden Apr 14 '21 at 16:47
  • 1
    Oh -- @BrianOgden -- I'd never do that! That would be silly. Thanks for making sure about this though! -- I am trying to set up the typescript project to use the root file of the project as the home directory for absolute paths - which is done a lot, from what I see. Mine is just not working yet. – Nick McLean Apr 15 '21 at 01:29

0 Answers0