raju
raju

Reputation: 6936

how to import module using '@' symbol, Node+Express+Typescript

I am relatively new to Node+Typescript, I have a file which import another file module using like this import { IS_PRODUCTION } from '@/config/config';, I cant understand how it is imported by @ symbol.

Need some help in understanding this.

Thanks

[EDIT]: this is tsconfig.json

{
  "compilerOptions": {
    "esModuleInterop": true,
    "target": "es2018",
    "noImplicitAny": true,
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "sourceMap": true,
    "outDir": "build",
    "baseUrl": ".",
    "paths": {
      "@/*": ["./src/*"],
      "@@/*": ["./*"]
    },
    "module": "commonjs",
    "skipLibCheck": true
  },
  "include": ["src/**/*"],
  "exclude": ["build", "node_modules"]
}

Upvotes: 9

Views: 10127

Answers (1)

Alex Wayne
Alex Wayne

Reputation: 187144

Your tsconfig.json has some custom paths setup. One of them is this:

"@/*": ["./src/*"],

That means that when typescript sees a path that starts with @/whatever then it translates that to path/of/tsconfig/here/src/whatever.

It's nice because it allows you load files with paths from the root of the project. That way if you move a file you don't have to change all the imports.

You can read about this feature in the documentation

Upvotes: 19

Related Questions