#Typescript aliases imports not working

3 messages · Page 1 of 1 (latest)

thorny spade
#

Hello everyone, I have some issues with my decorator's import using Typescript path aliases and swc.

This is an example of an import:
import { SkipAuth } from '@api/users/auth';

This is the content of src/api/users/auth/index.ts:
``export * from './decorators/skipAuth.decorator';`

This is the content of src/api/users/auth/decorators/skipAuth.decorator.ts:
export const SkipAuth = () => SetMetadata(SKIP_AUTH_KEY, true);

The IDE's intellisense doesn't give me any error but when I run nest start I get the following error:

@SkipAuth()
           ^
TypeError: (0 , _auth.SkipAuth) is not a function

SkipAuth seems to be undefined. I think there is something wrong in the build process but I can't figure out what.

Other (maybe) useful informations:
tsconfig path: "@api/users/*": ["src/api/users/*"],

Thanks in advance 🙂

river brook
#

I am essentially doing the same thing which works fine for me. Make sure your paths are set up correctly: that they are in the correct location in the tsconfig.json - I know I got this wrong at first and was struggling with it!

In my NestJS tsconfig.json file I have:

{
  "compilerOptions": {
    "baseUrl": "./",
    "target": "ES2022",
    "module": "commonjs",
    "allowSyntheticDefaultImports": true,
    "declaration": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "forceConsistentCasingInFileNames": true,
    "incremental": true,
    "outDir": "./dist",
    "removeComments": true,
    "skipLibCheck": true,
    "sourceMap": true,

    "paths": {
      "@shared-interfaces/*": ["../shared/interfaces/*"],
      "@shared-constants/*": ["../shared/constants/*"],
    },

    "strict": true,
    "noFallthroughCasesInSwitch": true
  }
}

In /shared/constants/*.ts files I would just have export const foo = 'bar';

And then within a NestJS file I would just import from that:

import { foo } from '@shared-constants/example-file';
thorny spade
#

I have no problems with other imports, the only thing that's not importing is the decorator