1

I want to import a config file with typescript, while avoiding using relative paths.

config.ts

export = {
var1: "hrqedfc",
var2: "gbrdf"
}

app.ts

const config = require("@app/config")
console.log(config)

I would like, if possible, to use the typescript way of importing things (import config from “config”)

The error I currently have is: Error: Cannot find module 'config'

NANO
  • 36
  • 6
  • Do you use any bundler? You can [setup aliases with webpack](https://webpack.js.org/configuration/resolve/) or [in Vite](https://vueschool.io/articles/vuejs-tutorials/import-aliases-in-vite/) etc... – Jax-p Jun 27 '22 at 15:13
  • Does this answer your question? https://stackoverflow.com/questions/55916731/using-absolute-paths-in-typescript-for-imports – Radu Diță Jun 27 '22 at 15:18

1 Answers1

0

You need to use tsconfig's path and baseUrl options. This tells the typescript compiler to substitute your @app string with its path relative to the root folder at build time. You get the benefit of being able to use a path alias "@app" by only setting the definition of @app at one point.

You can see this relevant answer: https://stackoverflow.com/a/44677624/12833553

You can also take a look at the documentation: https://www.typescriptlang.org/tsconfig#paths

BenMcLean981
  • 700
  • 5
  • 16